如何在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__
方法。
有没有更好的方法来定义抽象类?
发布于 2012-11-30 21:30:45
使用abc
模块创建抽象类。使用abstractmethod
装饰器声明方法抽象,并使用三种方法之一声明类抽象,具体取决于您的Python版本。
在Python3.4及更高版本中,您可以继承ABC
。在早期版本的Python中,需要将类的元类指定为ABCMeta
。在Python 3和Python 2中,指定元类的语法不同。下面显示了三种可能性:
# 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>
发布于 2014-08-14 12:33:20
老式的方法(在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
文档要容易一些。
发布于 2018-01-16 13:30:33
这里有一种非常简单的方法,不必处理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
这表明您可以实例化从基类继承的子类,但不能直接实例化基类。
https://stackoverflow.com/questions/13646245
复制相似问题