Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Get rid of recursive call __getattr__

Reply
Thread Tools

Get rid of recursive call __getattr__

 
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?

 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      12-14-2005
Pelmen wrote:
> 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.


>>> class Toto(object):

.... def __init__(self, name):
.... self.name = name
.... def __getattr__(self, attname):
.... print "__getattr__ called for %s" % attname
.... return "%s doesn't exists" % attname
....
>>> t = Toto('toto')
>>> t.name = name

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'name' is not defined
>>> t.name

'toto'
>>> t.age

__getattr__ called for age
"age doesn't exists"

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
 
Tim N. van der Leeuw
Guest
Posts: n/a
 
      12-14-2005

Pelmen wrote:
> 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

 
Reply With Quote
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
thanks, i should been read more closely

 
Reply With Quote
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
thanks, i understood my mistake
i try to get attribute, that wasn't defined

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      12-14-2005
Pelmen wrote:
> 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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

 
Reply With Quote
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
as __repr__ for example?

 
Reply With Quote
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
thanks, i found the problem

 
Reply With Quote
 
Pelmen
Guest
Posts: n/a
 
      12-14-2005
>>> class Test:
def __getattr__(self, attr):
print attr

def foo(x):
print x

>>> t = Test()
>>> print t

__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?

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-14-2005
Pelmen wrote:

> >>> class Test:

> **********def*__getattr__(self,*attr):
> ************print*attr
>
> **********def*foo(x):
> ************print*x
>
> >>> t = Test()
> >>> print t

> __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:

>>> class Test:

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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM
Does recursive call able to print in same page as main call Yohan N. Leder Perl Misc 19 07-02-2006 11:45 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM



Advertisments