例如:
class TestParent(object):
# I need a function to be compatible with both classmethod and instance method.
@property
def log(cls, self=None):
if "it's called from a child classmethod":
return logging.root.getChild(cls.__class__.__module__ + '.' + cls.__class__.__name__)
if "it's called from a child object":
return logging.root.getChild(self.__class__.__module__ + '.' + self.__class__.__name__)
class TestChild(TestParent):
@classmethod
def test(cls):
cls.logger.info('test')
def test2(self):
self.logger.info('test2')
child = TestChild()
child.test()
child.test2()
有什么办法可以做到这一点?
凤凰求蛊
相关分类