Gabriel Genellina wrote:
Ok, yes, I see
Yes, that works, thanks, I keep on forgetting that locals() exists!
En Wed, 20 Aug 2008 05:34:38 -0300, Gabriel Rossetti
<gabriel.rosset ti@arimaz.comes cribi�:
>
>
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)
<gabriel.rosset ti@arimaz.comes cribi�:
>
>I can't get getattr() to return nested functions, I tried this :
>>
>... 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 :
>>
> >>def toto():
>... pass
>... f = getattr(toto, "titi")
>... print str(f)
>...
> >>toto()
> 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)
>
>
Try using locals()[action]
>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(res ult)
>>
>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.
> 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(res ult)
>>
>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]