Python:抽象类 __init__
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44800659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: abstract class __init__
提问by dabadaba
I am trying to declare an abstract class A
with a constructor with a default behavior: all subclasses must initialize a member self.n
:
我正在尝试A
使用具有默认行为的构造函数声明一个抽象类:所有子类都必须初始化一个成员self.n
:
from abc import ABCMeta
class A(object):
__metaclass__ = ABCMeta
def __init__(self, n):
self.n = n
However, I do not want to let the A
class be instantiated because, well, it is an abstract class. The problem is, this is actually allowed:
但是,我不想让A
类被实例化,因为它是一个抽象类。问题是,这实际上是允许的:
a = A(3)
This produces no errors, when I would expect it should.
这不会产生错误,而我希望它应该。
So: how can I define an un-instantiable abstract class while defining a default behavior for the constructor?
那么:如何在为构造函数定义默认行为的同时定义不可实例化的抽象类?
回答by Mike Müller
Making the __init__
an abstract method:
使__init__
抽象方法:
from abc import ABCMeta, abstractmethod
class A(object):
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, n):
self.n = n
if __name__ == '__main__':
a = A(3)
helps:
帮助:
TypeError: Can't instantiate abstract class A with abstract methods __init__
Python 3 version:
Python 3 版本:
from abc import ABCMeta, abstractmethod
class A(object, metaclass=ABCMeta):
@abstractmethod
def __init__(self, n):
self.n = n
if __name__ == '__main__':
a = A(3)
Works as well:
也有效:
TypeError: Can't instantiate abstract class A with abstract methods __init__
回答by gipsy
A not so elegant solution can be this:
一个不太优雅的解决方案可能是这样的:
class A(object):
def __init__(self, n):
if self.__class__ == A:
raise Exception('I am abstract!')
self.n = n
Usage
用法
class B(A):
pass
a = A(1) # Will throw exception
b = B(1) # Works fine as expected.
回答by Ignacio Vergara Kausel
You should define the methods as abstract as well with the @abc.abstractmethod
decorator.
您应该使用@abc.abstractmethod
装饰器将方法定义为抽象方法。
回答by Ashwini Chaudhary
You can override __new__
method to prevent direct instantiation.
您可以覆盖__new__
方法以防止直接实例化。
class A(object):
__metaclass__ = ABCMeta
def __new__(cls, *args, **kwargs):
if cls is A:
raise TypeError(
"TypeError: Can't instantiate abstract class {name} directly".format(name=cls.__name__)
)
return object.__new__(cls)
Output:
输出:
>>> A()
Traceback (most recent call last):
File "<ipython-input-8-3cd318a12eea>", line 1, in <module>
A()
File "/Users/ashwini/py/so.py", line 11, in __new__
"TypeError: Can't instantiate abstract class {name} directly".format(name=cls.__name__)
TypeError: TypeError: Can't instantiate abstract class A directly
回答by Tiger
You can implement something like below :
您可以实现如下内容:
from abc import ABC
class A(ABC):
def __init__(self, n):
self.n = n
super(A,self).__init__()
Instantiating this class would throw an error
实例化这个类会抛出一个错误