我正在尝试在运行时向现有类添加一个新类(使用“type(...)”)。我还试图覆盖那个新类' __getattr__ 以便我可以对不在新类中的属性执行我自己的行为。例如,我有类 foo,我添加类“工具”,我希望 foo.tool.test 做我自己的事情。下面的代码有效,但只是部分有效。如果我显式调用 __getattr__,它可以工作(参见第一个打印),但是当我引用 foo.tool.test 时,我的覆盖 __getattr__ 不会被调用,并且会引发 attrbute 错误。
非常感谢您的帮助。
class Foo(object):
def __init__(self):
self.NameList=[]
# add new class to ourself
self.tool = type('tool', (object,), {} )
# override new class' __getattr__ with call to ourself
setattr(self.tool, "__getattr__", self.__getattr__ )
# add one well known test name for now
self.NameList.append( "test" )
# should be called by our newly added "tool" object but is only called sometimes...
def __getattr__(self, attr):
# print( "__getattr__: %s" % attr )
if( attr in self.NameList ):
return( 99 )
raise AttributeError("--%r object has no attribute %r" % (type(self).__name__, attr))
foo = Foo()
# access tool class attribute "test" - it should be seen by the override __getattr__
# the following works...
print( "foo.tool.__getattr__=%d" % foo.tool.__getattr__("test") )
# but the following does not - why is this not the same as the line above???
print( "foo.tool.test=%d" % foo.tool.test )