我用抽象类来解决这个问题。在我看来:from abc import ABCMeta, abstractmethod
class BasicClass(object):
__metaclass__ = ABCMeta
instance = None
def __new__(cls, *args, **kwargs):
if not cls.instance:
cls.instance = super(BasicClass, cls).__new__(
cls, *args, **kwargs)
return cls.instance
@abstractmethod
def action(self):
pass#do smthing
class FirstLevelChild(BasicClass):
__metaclass__ = ABCMeta
@abstractmethod
def action(self):
pass#do smthing in this or other abstract method
# also pearent method can be called with help of super
class SecondLevelChild1(FirstLevelChild):
def action(self):
pass#do smthing unique for this class
# in this or other abstract method
# also pearent method can be called with help of super
class SecondLevelChild2(FirstLevelChild):
def action(self):
pass#do smthing unique for this class
# in this or other abstract method
# also pearent method can be called with help of super
让我们检查一下
^{pr2}$
注意!如果您只需要实例化最后一级的子类,这个解决方案将有所帮助。你不能实例化抽象类。在