Get rid of recursive call __getattr__

P

Pelmen

How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?
 
B

bruno at modulix

Pelmen said:
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?
Sorry, but I don't understand your question. Which recursive calls to
__getattr__ ? __getattr__ is only called if a/ it's defined and b/ the
attribute has not been found (see below).

Have you overriden __setattr__ or __getattribute__ ? If yes, please read
the corresponding sections of the Fine Manual.

.... def __init__(self, name):
.... self.name = name
.... def __getattr__(self, attname):
.... print "__getattr__ called for %s" % attname
.... return "%s doesn't exists" % attname
....Traceback (most recent call last):
__getattr__ called for age
"age doesn't exists"
 
T

Tim N. van der Leeuw

Pelmen said:
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?

Hi Pelmen,

Having read the docs included with my Python distribution on
__getattr__, I don't see yet how you will get recursive calls to the
method... (It's called only when the attribute cannot be looked up via
normal means)

If you are seeing recursive calls to __getattr__, perhaps you can
highlight the problem with some sample-code?

regards,

--Tim
 
S

Steve Holden

Pelmen said:
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?
The usual mistake here is to write a __getattr__() implementation that
references an undefined self-relative name, which leads to a recursive
call of __getattr__(), which ...

regards
Steve
 
P

Pelmen

class Test:
def __getattr__(self, attr):
print attr

def foo(x):
print x
__str__

Traceback (most recent call last):
File "<pyshell#23>", line 1, in -toplevel-
print t
TypeError: 'NoneType' object is not callable

what i have to do? define __str__ explicitly?
 
P

Peter Otten

Pelmen said:
__str__

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in -toplevel-
    print t
TypeError: 'NoneType' object is not callable

what i have to do? define __str__ explicitly?

By seemingly not returning anything your __getattr__() method actually
returns None. Instead you should raise an AttributeError when your
__getattr__() encounters the name of an attribute it doesn't handle.
Let's assume Test.__getattr__() should implement an attribute 'alpha' and
nothing else:
.... def __getattr__(self, name):
.... print "looking up", name
.... if name == "alpha":
.... return 42
.... print "lookup failed for", name
.... raise AttributeError
....looking up __str__
lookup failed for __str__
looking up __repr__
lookup failed for __repr__
<__main__.Test instance at 0x4029248c>

When the lookup fails in the instance it is deferred to the class.
By the way, new-style classes handle __special__ methods a bit differently
-- try deriving Test from object

class Test(object):
# same as above

to see the difference.

Peter
 
S

Steve Holden

Peter said:
Pelmen wrote:




By seemingly not returning anything your __getattr__() method actually
returns None. Instead you should raise an AttributeError when your
__getattr__() encounters the name of an attribute it doesn't handle.
Let's assume Test.__getattr__() should implement an attribute 'alpha' and
nothing else:



... def __getattr__(self, name):
... print "looking up", name
... if name == "alpha":
... return 42
... print "lookup failed for", name
... raise AttributeError

or, rather better IMHO,

raise AttributeError("lookup failed for %s" % name)
regards
Steve
 
P

Peter Hansen

Pelmen said:
def __getattr__(self, attr):
print attr

def foo(x):
print x



__str__

Traceback (most recent call last):
File "<pyshell#23>", line 1, in -toplevel-
print t
TypeError: 'NoneType' object is not callable

what i have to do? define __str__ explicitly?

Yes. Or subclass "object" as it has a default __str__ already.

(By the way, you do realize that the NoneType message comes because your
__getattr__ is returning None, don't you? So technically you could also
return a real value (in this case a callable) and it would also work,
though it's very likely not what you wanted.

class Test:
def __getattr__(self, name):
def callable_attribute():
return 'i am attr %s' % name
return callable_attribute
i am attr __str__

-Peter
 
B

bruno at modulix

Steve said:
or, rather better IMHO,

raise AttributeError("lookup failed for %s" % name)

or still better in IMNSHO:
raise AttributeError("%s object has no attribute %s" %
\ (self.__class__.__name__,
name))

(which is the 'standard' AttributeError message)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top