471,309 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Get rid of recursive call __getattr__

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

Dec 14 '05 #1
13 3342
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 'o****@xiludom.gro'.split('@')])"
Dec 14 '05 #2

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

Dec 14 '05 #3
thanks, i should been read more closely

Dec 14 '05 #4
thanks, i understood my mistake
i try to get attribute, that wasn't defined

Dec 14 '05 #5
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/

Dec 14 '05 #6
as __repr__ for example?

Dec 14 '05 #7
thanks, i found the problem

Dec 14 '05 #8
>>> 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?

Dec 14 '05 #9
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

Dec 14 '05 #10
thanks, now all clear

Dec 14 '05 #11
Peter Otten wrote:
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


or, rather better IMHO,

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

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/

Dec 14 '05 #12
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?


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
t = Test()
print t

i am attr __str__

-Peter

Dec 14 '05 #13
Steve Holden wrote:
Peter Otten wrote:
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

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)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Dec 15 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by | last post: by
4 posts views Thread by Nicolas Vigier | last post: by
9 posts views Thread by Bill Borg | last post: by
4 posts views Thread by ThEoNeAnDOnLy | last post: by
3 posts views Thread by from.future.import | last post: by
3 posts views Thread by Davy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.