callability of object: strange behaviour
martin.schmettow at bibliothek.uni-regensburg.de
Wed Jan 15 15:00:50 CET 2003
And again me:
Never forget the
else: raise AttributeError, name
in an __getattr__ definition.
Martin Schmettow wrote:
> 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
> 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')
> return parent
> >>> node = Node('mynode')
> >>> callable(node)
> >>> node
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: 'NoneType' object is not callable
> >>> node.parent.name
> >>> 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.
More information about the Python-list