En Wed, 20 Aug 2008 05:34:38 -0300, Gabriel Rossetti
<ga************ **@arimaz.comes cribi�:
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(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]
--
Gabriel Genellina 0 1203 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:
(http://www.python.org/search/hypermail/python-1993/0343.html)
"This is because nested function definitions don't have access to the
local variables of the surrounding block -- only to the globals of the
|
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 could implement an
equivalent behaviour with plain C (in this case I'm thinking of
the language I'm developing, which shall be converted into C for
target compilation). So far I didn't touch the topic "nested
functions", since I just don't see an...
|
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 and its nested children all in one module,
declare the children static - voila. Std-C.
Regards,
Johannes
| |
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 (most recent call last):
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |