![](/img/trans.png)
[英]How to execute part of “__init__” after __init__ of subclass in python?
[英]in python is it possible to write a method that will be automatically called after __init__ of subclass
我正在写一个供他人使用的库。
对于每个重要功能,我都有一个基类。 用户应将其子类化,并添加其重要方法的版本。
现在,我想我需要添加一个“ setup()”函数,该函数必须在子类的init ()之后运行,但是那里已经存在不同的Problem
子类,并且我不想让人们碰触它们。
我也在2.7上开发。
说:
class Problem:
def __init__(self,**kwargs):
#this and that
def setup(self,**kwargs):
#Generic stuff that needs to be done after SpecificProblem.__init__() is called.
class SpecificProblem(Problem):
def __init__(self,**kwargs):
Problem.__init__(self,**kwargs)
#this and that
与其让子类覆盖__init__
,不如让它们编写一种称为make
的方法。 如:
class Problem:
def __init__(self,**kwargs):
self.make(**kwargs)
self.setup(**kwargs)
def setup(self,**kwargs):
#Generic stuff that needs to be done after SpecificProblem.__init__() is called.
def make(self,**kwargs):
# method to be overwritten by subclasses
pass
class SpecificProblem(Problem):
def make(self,**kwargs):
#this and that
有不同的方法来解决此问题。 如果您希望编写SpecificProblem的人员负责完成的一切以及完成的时间,那么元类方法将很有意义。 但是,如果您想适当地“分离关注点”,那么我的方法允许您这样做。
gabe的答案是应该使用的,但这是一个适合于元类的问题; 无需进一步说明:
class SetupClass(type):
def __call__(cls, *args, **kwargs):
# create the instance as normal. this will invokoe the class's
# __init__'s as expected.
self = super(SetupClass, cls).__call__(*args, **kwargs)
# this steps through the MRO in ascending order (so that child
# classes can be confident their bases are "set up"). A corresponding
# teardown() method should probably be `reversed(cls.__mro__)`
for base in cls.__mro__:
setup = vars(base).get('setup')
# in the general case, we have to use the descriptor protocol
# to setup methods/staticmethods/classmethods properly
if hasattr(setup, '__get__'):
setup = setup.__get__(self, cls)
if callable(setup):
setup()
return self
class A(object):
__metaclass__ = SetupClass
def __init__(self):
print "A.__init__"
def setup(self):
print "A.setup"
class B(A):
def __init__(self):
print "B.__init__"
super(B, self).__init__()
>>> B()
B.__init__
A.__init__
A.setup
只要打电话给Problem.setup
在__init__
的SpecificProblem
class SpecificProblem(Problem):
def __init__(self,**kwargs):
Problem.__init__(self,**kwargs)
#this and that
Problem.setup(self, **kwargs)
您确实应该在python中使用新型类,因为它们是python 3.x中的默认类。
class Problem(object):
def __init__(self, **kwargs):
pass
def setup(self, **kwargs):
pass
class SpecificProblem(Problem):
def __init__(self, **kwargs):
super(SpecificProblem, self).__init__(**kwargs)
# In python three, super().__init__(**kwargs) works.
super(SpecificProblem, self).setup(**kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.