How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class? 13 3445
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('@')])"
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
thanks, i should been read more closely
thanks, i understood my mistake
i try to get attribute, that wasn't defined
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/
thanks, i found the problem
>>> 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?
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
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/
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
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('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
OK:
Purpose: Using user's input and 3 recursive functions, construct an hour
glass figure. Main can only have user input, loops and function calls.
Recursive function 1 takes input and displays...
|
by: Nicolas Vigier |
last post by:
Hello,
I have in my python script a function that look like this :
def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
if type(arg1) is ListType:
for a in arg1:
my_function(a, arg2,...
|
by: Victor |
last post by:
Hello,
I've got a situation in which the number of (valid) recursive calls I
make will cause stack overflow. I can use getrlimit (and setrlimit)
to test (and set) my current stack size. ...
|
by: Bill Borg |
last post by:
Hello,
I call a function recursively to find an item that exists *anywhere* down
the chain. Let's say I find it five layers deep. Now I've got what I need and
want to break out of that whole...
|
by: glomde |
last post by:
Hi,
I tried to write a decorator for that should be for methods but for
some reasons
it doens seem to work when you try to do it on the __getattr__ method
in a class.
Could anybody give some...
|
by: ArseAssassin |
last post by:
I'm using minidom to parse XML and to simplify accessing child nodes, I'm trying to implement __getattr__ for the Node class. Yes, I know what most of you will think of that; I'll just have to be...
|
by: ThEoNeAnDOnLy |
last post by:
I recently had an issue with my recursive project in class. Here is the code.
// Recursion.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include...
|
by: from.future.import |
last post by:
Hi,
I encountered garbage collection behaviour that I didn't expect when
using a recursive function inside another function: the definition of
the inner function seems to contain a circular...
|
by: Davy |
last post by:
Hi all,
Sometimes I need to pass same parameter in recursive function. From my
point of view, the style is redundant, and I don't what to use some
global style like self.A, self.B, Is there any...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |