![](/img/trans.png)
[英]How to refer to subclass property in abstract shared implementation in abstract class method
[英]Define an abstract method but require a subclass implementation
请注意,我认为abc并不能解决我要找的问题。 以另一种也许更好的方式重述,我正在寻找一种部分实现Parent.method的方法,但要求也有一个Subclass.method,它使用并添加了部分Parent.method实现。
我想部分地定义一个抽象类方法,但仍要求该方法也必须在子类中实现。 例如:
class AbstractClass(object):
def amethod():
# some code that should always be executed here
vars = dosomething()
# But, since we're the "abstract" class
# force implementation through subclassing
if <somehow determine whether this has not been subclassed>:
raise NotImplementedError
class ActualClass(AbstractClass):
def amethod():
# Actual class code
code_here()
# And execute the super class code.
super(ActualClass, self).amethod()
像这样测试?
class AbstractClass(object):
def amethod(self):
# some code that should always be executed here
print(" AbstractClass.amethod()")
# But, since we're the "abstract" class
# force implementation through subclassing
if self.__class__ == AbstractClass:
raise NotImplementedError
class ActualClass(AbstractClass):
def amethod(self):
# Actual class code
print(" ActualClass.amethod()")
# And execute the super class code.
super(ActualClass, self).amethod()
#a = AbstractClass()
#a.amethod()
b = ActualClass()
b.amethod()
这也很有趣
def abstractmethod(method):
def default_abstract_method(*args, **kwargs):
raise NotImplementedError('call to abstract method ' + repr(method))
default_abstract_method.__name__ = method.__name__
return default_abstract_method
http://code.activestate.com/recipes/577666-abstract-method-decorator/
虽然我没有用过。
您可以通过在父级中引发异常来强制执行此操作:
class base(object):
def a_method(self):
raise NotImplementedError("Implement this!")
class second(base):
pass
如果我调用second().a_method()
我将得到一个异常。 Python中没有抽象的东西,但这可能是您最好的方法。 除此以外,
import abc
class base(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def something_to_implement(this):
"""A docstring"""
return
这将通过尝试引发TypeError
如果已初始化)来使方法“抽象”。
请注意,我认为abc并不能解决我要找的问题。
实际上abc
正是您要寻找的。 在基类中定义一个实现,但将其装饰为抽象,则需要派生类来重新定义它。 当然,这具有阻止您实例化基类的副作用,我认为这在您的用例中是可以的。
import abc
# inheritance from abc.ABC is important, as otherwise the decorators don't do anything
class AbstractClass(abc.ABC):
@abc.abstractmethod
def amethod(self):
# some code that should always be executed here
print("Base implementation")
class ActualClass(AbstractClass):
# will return TypeError: Can't instantiate abstract class ActualClass with abstract methods amethod if not redefined
def amethod(self):
# Actual class code
print("Actual implementation")
# And execute the super class code. (only one super class so less confusing)
super().amethod()
a = ActualClass()
a.amethod()
我习惯称此为“空白填充模式”(注意:这不是设计模式)。 您可以在抽象类中定义一个具体方法,该类调用抽象方法并用作带有“空白”的模板(抽象方法)。 子类“填充空白”,实现抽象方法。 在您的简单情况下:
class AbstractClass(object):
def amethod(self):
self._amethod()
print('Common operations')
@abc.abstractmethod
def _amethod(self, vars):
pass
class ConcreteClass(AbstractClass):
def _amethod(self):
print('Concrete class code')
通常,您可以给抽象方法起一个更好的名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.