En Wed, 20 Aug 2008 05:34:38 -0300, Gabriel Rossetti
<ga**************@arimaz.comescribi�:
I can't get getattr() to return nested functions, I tried this :
>>def toto():
... def titi():
... pass
... f = getattr(toto, "titi")
... print str(f)
...
>>toto()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in toto
AttributeError: 'function' object has no attribute 'titi'
>>>
I thought that since functions are objects, that I could obtain it's
nested functions. How come it doesn't work and what can I do to
fix/replace it? I'm using it in code that is like this :
Yes, functions are objects, but inner functions aren't attributes of the
outer; they live in its local namespace instead (and inner functions won't
exist until the outer function executes)
def __test(self, action, *args):
def request(params):
pass
def submit(params, values):
pass
def update(params, values):
pass
def delete(params):
pass
result = getattr(__test, action)(*args)
return resultToXml(result)
where "action" is a string containing either "request", "submit",
"update", or "delete". I was using an evel() with this form :
result = eval(action + "(params, values)")
but I didn't find that very clean.
Try using locals()[action]
--
Gabriel Genellina 0 1170 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy Baker |
last post by:
Hi there,
I'm learning Python at the moment and trying to grok the thinking behind
it's scoping and nesting rules.
I was googling for nested functions and found this Guido quote:...
|
by: block111 |
last post by:
Hello,
code like this:
int f1(int x){
int f2(int y){
return y*y;
}
if(x > 0)
return f2(x);
|
by: Wolfgang Draxinger |
last post by:
If you know languages like Python or D you know, that nested
functions can be really handy.
Though some compilers (looking at GCC) provide the extension of
nested functions, I wonder, how one...
|
by: Rotlaus |
last post by:
Hello,
lets assume i have some classes:
class A(object):
def __init__(self):
b = B()
class B(object):
def __init__(self):
|
by: Johannes Bauer |
last post by:
Nick Keighley schrieb:
Why is there actually a *need* for nested functions? If functionality of
subfunctions which are only locally visible is desired, why not put the
nesting function parent...
|
by: Gregor Horvath |
last post by:
Hi,
class A(object):
test = "test"
class B(object):
a = A()
In : B.a.test
|
by: Gabriel Rossetti |
last post by:
Hello,
I can't get getattr() to return nested functions, I tried this :
.... def titi():
.... pass
.... f = getattr(toto, "titi")
.... print str(f)
....
Traceback...
|
by: Gabriel Rossetti |
last post by:
Gabriel Genellina wrote:
Ok, yes, I see
Yes, that works, thanks, I keep on forgetting that locals() exists!
|
by: maestro |
last post by:
Why are these functions there? Is it somehow more idiomatic to use
than to do obj.field ?
Is there something you can with them that you can't by obj.field
reference?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |