Is except: ... pass bad style?
Heiko Wundram
heikowu at ceosg.de
Thu Sep 9 17:07:49 EDT 2004
Am Donnerstag, 9. September 2004 19:56 schrieb marduk:
> myobj.__dict__.get('method', lambda : None)()
This won't work when inheritance is in play. See for yourself:
>>> class x(object):
... def method(self):
... return "I was called!"
...
>>> class y(x):
... pass
...
>>> a = y()
>>> a.method()
'I was called!'
>>> a.__dict__.get("method",lambda: None)()
>>> a.__dict__.get("method")
So, use getattr, which properly walks the inheritance tree:
>>> getattr(a,"method",lambda: None)()
'I was called!'
Heiko.
More information about the Python-list
mailing list