__getattr__ and __setattr__ troubles
François Pinard
pinard at iro.umontreal.ca
Tue Jun 6 14:46:05 EDT 2000
"Adam Ruth" <aruth at intercation.com> writes:
> The __str__ and __coerce__, etc, will all be sent to __getattr__ because
> they are *not* defined in the class. If they were, then __getattr__
> would never be called.
But would not this get fairly tedious? For example:
---------------------------------------------------------------------->
class See: pass
print See()
----------------------------------------------------------------------<
actually yields:
---------------------------------------------------------------------->
pinard at titan:~ $ python see.py
<__main__.See instance at 81898b8>
----------------------------------------------------------------------<
which is convenient enough for me. While a simple:
---------------------------------------------------------------------->
class See:
def __getattr__(self, key):
print '**', key
return None
print See()
----------------------------------------------------------------------<
will yield:
---------------------------------------------------------------------->
pinard at titan:~ $ python see.py
** __str__
Traceback (innermost last):
File "see.py", line 7, in ?
print See()
TypeError: call of non-function (type None)
----------------------------------------------------------------------<
* By the way, and a bit unrelated, why the space before the two asterisks?
* The diagnostic would be rather cryptic, if the program was not so short.
Usability studies could be driven to show that the diagnostic should rather
be changed to something more informative :-).
* May I least get the default string representation, which was satisfying?
* So, as soon as I use `__getattr__', I suddenly get a lot of other things to
define as well? This would make `__getattr__' less attractive.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list