在python中,我可以创建一个在实例化时可以接收任意方法调用的类吗?我已阅读此内容,但无法将各个部分拼凑在一起
我想这与attribute lookup
. 对于一个类Foo
:
class Foo(object):
def bar(self, a):
print a
类属性可以通过 获得print Foo.__dict__
,这给出
{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__module__': '__main__', 'bar': <function bar at 0x7facd91dac80>, '__doc__': None}
所以这段代码是有效的
foo = Foo()
foo.bar("xxx")
如果我打电话foo.someRandomMethod()
,AttributeError: 'Foo' object has no attribute 'someRandomMethod'
会导致。
我希望foo
对象接收任何随机调用并默认为无操作,即。
def func():
pass
我怎样才能做到这一点?我希望这种行为模拟一个对象进行测试。