__getattr__ and functions that don't exist

Erik Johnson nobody
Fri May 26 00:17:43 CEST 2006


Maybe I just don't know the right special function, but what I am wanting to
do is write something akin to a __getattr__ function so that when you try to
call an object method that doesn't exist, it get's intercepted *along with
it's argument*, in the same manner as __getattr__ intercepts attributes
references for attributes that don't exist.


This doesn't quite work:

>>> class Foo:
...   def __getattr__(self, att_name, *args):
...     print "%s%s" % (att_name, str(tuple(*args)))
...
>>> f = Foo()
>>> f.bar
bar()
>>> f.bar(1,2,3)
bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable
>>> f.bar()
bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable


Is there some other special function like __getattr__ that does what I want?

Thanks,
-ej





More information about the Python-list mailing list