我尝试像这样重新定义`__new__()`:
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
但是现在,如果我创建一个从
F
继承的类G
,就像这样:class G(F):
pass
然后,我也无法实例化G
,因为它调用了其超类的__new__
方法。
有没有更好的定义抽象类的方式?
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
F
继承的类G
,就像这样:class G(F):
pass
然后,我也无法实例化G
,因为它调用了其超类的__new__
方法。
有没有更好的定义抽象类的方式?
abc
模块创建抽象类。使用abstractmethod
装饰器声明一个方法为抽象方法,并根据你的Python版本使用三种不同的方式之一来声明一个抽象类。ABC
。在早期的Python版本中,则需要将你的类的元类指定为ABCMeta
。在Python 2和Python 3中,指定元类的语法不同。以下是三种可能性:# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
@abstractmethod
def foo(self):
pass
# Python 3.0+
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
# Python 2
from abc import ABCMeta, abstractmethod
class Abstract:
__metaclass__ = ABCMeta
@abstractmethod
def foo(self):
pass
>>> Abstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Abstract with abstract methods foo
>>> class StillAbstract(Abstract):
... pass
...
>>> StillAbstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class StillAbstract with abstract methods foo
>>> class Concrete(Abstract):
... def foo(self):
... print('Hello, World')
...
>>> Concrete()
<__main__.Concrete object at 0x7fc935d28898>
@abstractmethod
装饰的函数必须在类被实例化之前被重写。根据文档:如果一个类的元类派生自 ABCMeta,则该类不能被实例化,除非它的所有抽象方法和属性都被重写。 - Fake Name@abstractmethod
而不使用ABC
?这感觉不符合Python的风格。当我写abstractmethod
时,它意味着这已经是一个抽象基类了。或者我有什么遗漏吗? - hans在PEP 3119之前,传统的做法是在抽象类中调用抽象方法时使用raise NotImplementedError
。
class Abstract(object):
def foo(self):
raise NotImplementedError('subclasses must override foo()!')
class Derived(Abstract):
def foo(self):
print 'Hooray!'
>>> d = Derived()
>>> d.foo()
Hooray!
>>> a = Abstract()
>>> a.foo()
Traceback (most recent call last): [...]
abc
模块那样好的特性。你仍然可以实例化抽象基类本身,但只有在运行时调用抽象方法时才会发现错误。abc
文档要容易一些。Abstract#foo
中实现一些常见的行为。直接调用它应该被禁止,但仍然可以通过super()
调用它。 - Eric Duminil这里有一种非常简单的方法,无需使用ABC模块。
在你想要将其作为抽象类的类的__init__
方法中,你可以检查self的“type”。如果self的类型是基类,则调用者正在尝试实例化基类,因此应该引发异常。下面是一个简单的示例:
class Base():
def __init__(self):
if type(self) is Base:
raise Exception('Base is an abstract class and cannot be instantiated directly')
# Any initialization code
print('In the __init__ method of the Base class')
class Sub(Base):
def __init__(self):
print('In the __init__ method of the Sub class before calling __init__ of the Base class')
super().__init__()
print('In the __init__ method of the Sub class after calling __init__ of the Base class')
subObj = Sub()
baseObj = Base()
运行时,它会产生:
In the __init__ method of the Sub class before calling __init__ of the Base class
In the __init__ method of the Base class
In the __init__ method of the Sub class after calling __init__ of the Base class
Traceback (most recent call last):
File "/Users/irvkalb/Desktop/Demo files/Abstract.py", line 16, in <module>
baseObj = Base()
File "/Users/irvkalb/Desktop/Demo files/Abstract.py", line 4, in __init__
raise Exception('Base is an abstract class and cannot be instantiated directly')
Exception: Base is an abstract class and cannot be instantiated directly
这表明您可以实例化一个继承自基类的子类,但不能直接实例化基类。
这表明您可以创建一个继承自基类的子类的实例,但不能直接创建基类的实例。
大多数之前的答案都是正确的,但这里是Python 3.7的答案和示例。是的,你可以创建一个抽象类和方法。只要提醒一下,有时候一个类应该定义一个方法,这个方法在逻辑上属于这个类,但是这个类不能指定如何实现这个方法。例如,在下面的Parents和Babies类中,它们都吃东西,但是实现方式对每个类来说都不同,因为婴儿和父母吃的食物不同,并且吃饭的次数也不同。因此,eat方法子类覆盖了AbstractClass.eat。
from abc import ABC, abstractmethod
class AbstractClass(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def eat(self):
pass
class Parents(AbstractClass):
def eat(self):
return "eat solid food "+ str(self.value) + " times each day"
class Babies(AbstractClass):
def eat(self):
return "Milk only "+ str(self.value) + " times or more each day"
food = 3
mom = Parents(food)
print("moms ----------")
print(mom.eat())
infant = Babies(food)
print("infants ----------")
print(infant.eat())
输出:
moms ----------
eat solid food 3 times each day
infants ----------
Milk only 3 times or more each day
super().__init__()
? - Shlomi A如其他回答所述,是的,您可以使用Python中的抽象类,使用abc
模块。下面我将使用抽象@classmethod
、@property
和@abstractmethod
给出一个实际的示例(使用Python 3.6+)。对于我来说,通常更容易从我可以轻松复制和粘贴的示例开始。希望这个答案对其他人也有用。
让我们首先创建一个名为Base
的基类:
from abc import ABC, abstractmethod
class Base(ABC):
@classmethod
@abstractmethod
def from_dict(cls, d):
pass
@property
@abstractmethod
def prop1(self):
pass
@property
@abstractmethod
def prop2(self):
pass
@prop2.setter
@abstractmethod
def prop2(self, val):
pass
@abstractmethod
def do_stuff(self):
pass
我们的Base
类将始终拥有一个from_dict
类方法,一个只读的prop1
属性,一个可读可写的prop2
属性以及一个名为do_stuff
的函数。现在基于Base
构建的任何类都必须实现这四个方法/属性。请注意,为了使方法成为抽象方法,需要使用两个装饰器-classmethod
和abstract
property
。
现在我们可以创建一个A
类如下:
class A(Base):
def __init__(self, name, val1, val2):
self.name = name
self.__val1 = val1
self._val2 = val2
@classmethod
def from_dict(cls, d):
name = d['name']
val1 = d['val1']
val2 = d['val2']
return cls(name, val1, val2)
@property
def prop1(self):
return self.__val1
@property
def prop2(self):
return self._val2
@prop2.setter
def prop2(self, value):
self._val2 = value
def do_stuff(self):
print('juhu!')
def i_am_not_abstract(self):
print('I can be customized')
所有必需的方法/属性都已实现,当然我们也可以添加不属于 Base
的附加函数(这里是:i_am_not_abstract
)。
现在我们可以执行:
a1 = A('dummy', 10, 'stuff')
a2 = A.from_dict({'name': 'from_d', 'val1': 20, 'val2': 'stuff'})
a1.prop1
# prints 10
a1.prop2
# prints 'stuff'
按照要求,我们无法设置prop1
:
a.prop1 = 100
将返回
属性错误: 无法设置属性
同时我们的 from_dict
方法工作正常:
a2.prop1
# prints 20
如果我们现在定义了第二个类 B
,如下所示:class B(Base):
def __init__(self, name):
self.name = name
@property
def prop1(self):
return self.name
并尝试像这样实例化一个对象:
b = B('iwillfail')
我们将会遇到一个错误
类型错误:无法用抽象方法 do_stuff,from_dict,prop2 实例化抽象类 B,来自于 Base
列出在 B
中未实现的所有在 Base
中定义的内容。
@abstractclassmethod
从3.3版本开始已被弃用。 - Cleb这个需要在Python 3中工作。
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
Abstract()
>>> TypeError: Can not instantiate abstract class Abstract with abstract methods foo
from abc import ABC
和 class MyABC(ABC)
。 - user26742873同时这个方法也很简单:
class A_abstract(object):
def __init__(self):
# quite simple, old-school way.
if self.__class__.__name__ == "A_abstract":
raise NotImplementedError("You can't instantiate this abstract class. Derive it, please.")
class B(A_abstract):
pass
b = B()
# here an exception is raised:
a = A_abstract()
class F:
def __new__(cls):
if cls is F:
raise TypeError("Cannot create an instance of abstract class '{}'".format(cls.__name__))
return super().__new__(cls)
self
传递给一个抽象类。抽象类不能被实例化,因此不能有 self
。所以试试这个方法,它可以工作。from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def foo():
"""An abstract method. No need to write pass"""
class Derived(Abstract):
def foo(self):
print('Hooray!')
FOO = Derived()
FOO.foo()
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
要使用抽象类,需要通过子类继承它,并且子类需要覆盖抽象类中的抽象方法,如下所示:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Cat(Animal): # Extends "Animal" abstract class
def sound(self): # Overrides "sound()" abstract method
print("Meow!!")
obj = Cat()
obj.sound()
输出:
Meow!!
抽象方法可以包含代码,而不仅仅是pass
语句,子类可以像下面展示的那样调用它:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
print("Wow!!") # Here
class Cat(Animal):
def sound(self):
super().sound() # Here
obj = Cat()
obj.sound()
输出:
Wow!!
抽象类可以拥有变量和非抽象方法,这些可以被子类调用,而且非抽象方法不需要被子类重写,如下所示:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
def __init__(self): # Here
self.name = "John" # Here
x = "Hello" # Here
def test1(self): # Here
print("Test1")
@classmethod # Here
def test2(cls):
print("Test2")
@staticmethod # Here
def test3():
print("Test3")
class Cat(Animal):
def sound(self):
print(self.name) # Here
print(super().x) # Here
super().test1() # Here
super().test2() # Here
super().test3() # Here
obj = Cat()
obj.sound()
输出:
John
Hello
Test1
Test2
Test3
同时,你可以定义一个抽象类和静态方法,以及在抽象类中定义抽象的getter、setter和deleter,如下所示。必须将@abstractmethod
放在最内层装饰器位置,否则会导致错误,你可以参考我的回答,其中更详细地解释了抽象的getter、setter和deleter:
from abc import ABC, abstractmethod
class Person(ABC):
@classmethod
@abstractmethod # The innermost decorator
def test1(cls):
pass
@staticmethod
@abstractmethod # The innermost decorator
def test2():
pass
@property
@abstractmethod # The innermost decorator
def name(self):
pass
@name.setter
@abstractmethod # The innermost decorator
def name(self, name):
pass
@name.deleter
@abstractmethod # The innermost decorator
def name(self):
pass
class Student(Person):
def __init__(self, name):
self._name = name
@classmethod
def test1(cls): # Overrides abstract class method
print("Test1")
@staticmethod
def test2(): # Overrides abstract static method
print("Test2")
@property
def name(self): # Overrides abstract getter
return self._name
@name.setter
def name(self, name): # Overrides abstract setter
self._name = name
@name.deleter
def name(self): # Overrides abstract deleter
del self._name
obj = Student("John") # Instantiates "Student" class
obj.test1() # Class method
obj.test2() # Static method
print(obj.name) # Getter
obj.name = "Tom" # Setter
print(obj.name) # Getter
del obj.name # Deleter
print(hasattr(obj, "name"))
输出:
Test1
Test2
John
Tom
False
如果您尝试像下面这样实例化一个抽象类:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
obj = Animal()
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Cat(Animal):
pass # Doesn't override "sound()" abstract method
obj = Cat() # Here
出现以下错误:
TypeError: 无法实例化具有声音抽象方法的抽象类 Cat
如果在不扩展 ABC
的非抽象类中定义了一个抽象方法,则该抽象方法是普通实例方法,因此即使实例化非抽象类并且子类未覆盖非抽象类的抽象方法,也不会出现错误,如下所示:
from abc import ABC, abstractmethod
class Animal: # Doesn't extend "ABC"
@abstractmethod # Here
def sound(self):
print("Wow!!")
class Cat(Animal):
pass # Doesn't override "sound()" abstract method
obj1 = Animal() # Here
obj1.sound()
obj2 = Cat() # Here
obj2.sound()
输出:
Wow!!
Wow!!
Animal
类的Cat
类:from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
# ↓↓↓ Here ↓↓↓
class Cat(Animal):
def sound(self):
print("Meow!!")
# ↑↑↑ Here ↑↑↑
print(issubclass(Cat, Animal))
以下是具有register()的代码:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
# ↓↓↓ Here ↓↓↓
class Cat:
def sound(self):
print("Meow!!")
Animal.register(Cat)
# ↑↑↑ Here ↑↑↑
print(issubclass(Cat, Animal))
那么,上面两段代码的输出结果都相同,显示Cat
类是Animal
类的子类:
True