[Python-Dev] Python 2.2a* getattr suggestion and question

Roman Suzi rnd@onego.ru
Sun, 30 Sep 2001 16:19:18 +0400 (MSD)


Well, now every attr access goes thru __getattr__-method,
so this could cause situations which give not so clear
diagnostics:

-----------------------------------------------------------------

Python 2.2a3 (#1, Sep 26 2001, 22:42:46)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
HELP loaded. Readline loaded. History loaded.
>>> class A:
...  def aa(self):
...    print self
...
>>> class B(A):
...   def __getattr__(self, x):
...     print "getattr:", x
...
>>> b = B()
>>> b.aa
getattr: __repr__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object is not callable: None
-----------------------------------------------------------------

The problem above is that __repr__ can't be called because
__getattr__ intercepts it's call, giving None.

Could something be done with this to make it easy to
trace such kinds of problems?

Also, the question is what is the proper way (1 and only 1 way) to check
if attribute exists inside __getattr__ method? How can it be done by one
simple check like:

  def __getattr__(self, attr):
    if hasattr(self, attr):
      ....

Or do I need some other tricks?

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd@onego.ru _/
_/ Sunday, September 30, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Killer Rabbit's Motto: "Lettuce Prey."" _/