Python Macros

David Bolen db3l at
Wed Oct 6 19:57:32 CEST 2004

Jeff Shannon <jeff at> writes:

> In addition to redefining __getattr__()/__setattr__(), as has already
> been suggested, it's even easier to have your calling code (which
> probably knows more about how to recover from failure than the called
> code) decide what to do.
> try:
> except AttributeError:

One thing that you need to be aware of with this approach is that your
exception handler can catch an underlying AttributeError below the
level you probably intend when you write this code.  So, for example,
if foo _does_ have a bar method, but when bar is running it does
something to raise an AttributeError, the caller might mistakenly
think this meant foo had no method.

Whether or not that matters in practice of course depends on the
application at hand, and it's probably a bug in any case, unless bar()
is supposed to be able to raise AttributeError.  But it can represent
a difference from having the foo overload __getattr__, since in that
case you concretely know that foo does not have an attribute and
forwarding on the request to another object won't risk duplicating
partial operations.

-- David

More information about the Python-list mailing list