如何在Python中使类或方法抽象?
我尝试像这样重新定义__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__方法。
是否有更好的方法来定义抽象类?
只是对@TimGilbert老派回答的一个快速补充…你可以让你的抽象基类的init()方法抛出一个异常,这将阻止它被实例化,不是吗?
>>> class Abstract(object):
... def __init__(self):
... raise NotImplementedError("You can't instantiate this class!")
...
>>> a = Abstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
NotImplementedError: You can't instantiate this class!
这里有一个不需要处理ABC模块的非常简单的方法。
在你想要成为抽象类的类的__init__方法中,你可以检查self的“类型”。如果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
这表明您可以实例化继承自基类的子类,但不能直接实例化基类。
Most Previous answers were correct but here is the answer and example for Python 3.7. Yes, you can create an abstract class and method. Just as a reminder sometimes a class should define a method which logically belongs to a class, but that class cannot specify how to implement the method. For example, in the below Parents and Babies classes they both eat but the implementation will be different for each because babies and parents eat a different kind of food and the number of times they eat is different. So, eat method subclasses overrides 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
老式的(pre-PEP 3119)方法是在调用抽象方法时在抽象类中引发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文档要容易一些。