callability of object: strange behaviour

Martin Schmettow martin.schmettow at bibliothek.uni-regensburg.de
Wed Jan 15 08:02:53 EST 2003


Hi anybody.

I'm working on a class which implements a tree by fetching the nodes 
from a database on demand. A node once fetched should be cached 
transparently.
Implementing a method parent() I experienced a very strange behaviour of 
my object with callability.

This is the extract of what happens:

class Node:
     def __init__(self,name):
         self.name = name

     def __getattr__(self,name):
         if name == 'parent':
             parent = Node('parent')
             setattr(self,'parent',parent)
             return parent


 >>> node = Node('mynode')
 >>> callable(node)
1
 >>> node
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable
 >>> node.parent.name
'parent'
 >>> node.parent.__class__
<class __main__.Node at 0x80fe154>
 >>>

What's going wrong? Can anyone explain this behaviour to me?
Meanwhile I will try, if constructing the parent with new.instance helps.

CU
Martin.






More information about the Python-list mailing list