469,643 Members | 1,753 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Re: getattr() on nested functions?

En Wed, 20 Aug 2008 05:34:38 -0300, Gabriel Rossetti
I can't get getattr() to return nested functions, I tried this :
>>def toto():
... def titi():
... pass
... f = getattr(toto, "titi")
... print str(f)
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):
def submit(params, values):
def update(params, values):
def delete(params):
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

Aug 20 '08 #1
0 896

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Andy Baker | last post: by
7 posts views Thread by block111 | last post: by
4 posts views Thread by Wolfgang Draxinger | last post: by
3 posts views Thread by Rotlaus | last post: by
2 posts views Thread by Johannes Bauer | last post: by
8 posts views Thread by Gregor Horvath | last post: by
9 posts views Thread by Gabriel Rossetti | last post: by
reply views Thread by Gabriel Rossetti | last post: by
4 posts views Thread by maestro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.