可以用Python生成抽象类吗?
如何在Python中抽象类或方法 我试着重新定义可以用Python生成抽象类吗?,python,class,inheritance,abstract-class,abstract,Python,Class,Inheritance,Abstract Class,Abstract,如何在Python中抽象类或方法 我试着重新定义\uuuu new\uuuu()如下: class F: def __new__(cls): raise Exception("Unable to create an instance of abstract class %s" %cls) class G(F): pass 但是现在如果我创建一个继承自F的类G,如下所示: class F: def __new__(cls): raise
\uuuu new\uuuu()
如下:
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
class G(F):
pass
但是现在如果我创建一个继承自F
的类G
,如下所示:
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
class G(F):
pass
然后我也不能实例化G
,因为它调用了它的超类的\uuuuu new\uuuu
方法
有更好的方法定义抽象类吗?使用模块创建抽象类。根据您的Python版本,使用decorator声明方法抽象,并使用三种方法之一声明类抽象
在Python3.4及更高版本中,您可以从继承。在Python的早期版本中,需要将类的元类指定为。在Python3和Python2中,指定元类的语法不同。三种可能性如下所示:
# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
@abstractmethod
def foo(self):
pass
无论使用哪种方式,都不能实例化具有抽象方法的抽象类,但可以实例化提供这些方法具体定义的子类:
>>摘要()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法使用抽象方法foo实例化抽象类抽象
>>>类抽象(抽象):
... 通过
...
>>>StillAbstract()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法使用抽象方法foo实例化抽象类StillAbstract
>>>具体类别(摘要):
... def foo(self):
... 打印(‘你好,世界’)
...
>>>混凝土()
在代码片段中,您还可以通过在子类中提供\uuuu new\uuuu
方法的实现来解决此问题,同样:
def G(F):
def __new__(cls):
# do something here
但这是一个黑客,我建议你反对它,除非你知道你在做什么。对于几乎所有情况,我建议您使用前面的其他人建议的abc
模块
另外,当您创建一个新的(基类)类时,将其设为子类object
,如下所示:classmybaseclass(object):
。我不知道它是否还有那么重要,但它有助于保持代码的风格一致性老派的(预)方法就是在调用抽象方法时在抽象类中提出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
文档要容易一些。这一种方法将在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
只是对@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!
类抽象(对象):
... 定义初始化(自):
... raise NOTEImplementedError(“无法实例化此类!”)
...
>>>a=摘要()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在_init中__
NotImplementedError:您不能实例化这个类!
这也很有效,而且很简单:
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()
这里有一个非常简单的方法,不必处理ABC模块 在希望成为抽象类的类的
\uuuuu init\uuuu
方法中,可以检查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()
运行时,它会生成:
调用基类的uuu init_uuu之前,在子类的uuu init_uu方法中使用
在基类的uu init_uu方法中
在调用基类的uuu init_uuu之后,在子类的uuu init_uuu方法中
回溯(最近一次呼叫最后一次):
文件“/Users/irvkalb/Desktop/Demo files/Abstract.py”,第16行,在
baseObj=Base()
文件“/Users/irvkalb/Desktop/Demo files/Abstract.py”,第4行,在__
引发异常('Base是一个抽象类,不能直接实例化')
异常:Base是一个抽象类,不能直接实例化
这表明您可以实例化从基类继承的子类,但不能直接实例化基类。以前的大多数答案都是正确的,但下面是Python 3.7的答案和示例。是的,您可以创建一个抽象类和方法。提醒一下,有时一个类应该定义一个逻辑上属于某个类的方法,但该类不能指定如何实现该方法。例如,在下面的家长和婴儿课程中,他们都吃东西,但每个人的实施方式不同,因为婴儿和家长吃的食物种类不同,吃的次数也不同。因此,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
您还可以利用“新”方法为您带来优势。你忘了什么。 __new__方法始终返回新对象,因此必须返回其超类的new method。做如下事情
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)
使用新方法时,必须返回对象,而不是None关键字。这就是您所错过的。正如其他答案中所解释的,是的,您可以使用Python中的抽象类。下面我给出了一个使用抽象和(使用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
classmethod
、一个属性prop1
(只读)和一个属性prop2
(也可以设置)以及一个名为do_stuff
的函数。不管怎样
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')
a1 = A('dummy', 10, 'stuff')
a2 = A.from_dict({'name': 'from_d', 'val1': 20, 'val2': 'stuff'})
a1.prop1
# prints 10
a1.prop2
# prints 'stuff'
a.prop1 = 100
a2.prop1
# prints 20
class B(Base):
def __init__(self, name):
self.name = name
@property
def prop1(self):
return self.name
b = B('iwillfail')
from abc import ABCMeta, abstractmethod
#Abstract class and abstract method declaration
class Jungle(metaclass=ABCMeta):
#constructor with default values
def __init__(self, name="Unknown"):
self.visitorName = name
def welcomeMessage(self):
print("Hello %s , Welcome to the Jungle" % self.visitorName)
# abstract method is compulsory to defined in child-class
@abstractmethod
def scarySound(self):
pass
# decorators.py
def abstract(f):
def _decorator(*_):
raise NotImplementedError(f"Method '{f.__name__}' is abstract")
return _decorator
# yourclass.py
class Vehicle:
def addGas():
print("Gas added!")
@abstract
def getMake(): ...
@abstract
def getModel(): ...