在python中,我可以创建一个类,当实例化时,可以接收任意方法调用吗?我读过this,但不能把它们拼凑起来
我想这和attribute lookup
有关。对于类Foo
:
class Foo(object):
def bar(self, a):
print a
class属性可以通过print Foo.__dict__
获得,它给出了
所以这个代码是有效的
foo = Foo()
foo.bar("xxx")
如果我调用foo.someRandomMethod()
,AttributeError: 'Foo' object has no attribute 'someRandomMethod'
将产生。在
我希望foo
对象接收任何随机调用,并默认为no op,即
def func():
pass
我怎样才能做到这一点?我想用这个行为来模拟一个测试对象。在
来自http://rosettacode.org/wiki/Respond_to_an_unknown_method_call#Python
相关问题 更多 >
编程相关推荐