472,951 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Can you use -getattr- to get a function in the current module?

This works:

# Module spam.py

import eggs

print getattr(eggs, 'omelet')(100)

That is, I just call the function omelet inside the module eggs and
evaulate it with the argument 100.

But what if the function 'omelet' is in the module where I do the
getattr (that is, in spam.py). If I do any of this

print getattr(spam, 'omelet')(100)
print getattr('','omelet')(100)
print getattr('omelet')(100)

It wont work. Any ideas?

Thanks,
Sergio
Sep 4 '07 #1
1 19394
Sergio Correia <se************@gmail.comwrote:
This works:

# Module spam.py

import eggs

print getattr(eggs, 'omelet')(100)

That is, I just call the function omelet inside the module eggs and
evaulate it with the argument 100.

But what if the function 'omelet' is in the module where I do the
getattr (that is, in spam.py). If I do any of this

print getattr(spam, 'omelet')(100)
print getattr('','omelet')(100)
print getattr('omelet')(100)

It wont work. Any ideas?
globals() returns a dict of all globals defined so far, so, _after_ 'def
omelet ...' has executed, globals()['omelet'](100) should be OK.
Alex
Sep 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fernando Rodriguez | last post by:
Hi, How can I get the list of all the functions defined in the current module?
0
by: Christian Hudon | last post by:
Hi, I'd like to be able to trap lookups of attributes of the current module. Exactly like what __getattr__ does, but for modules. As an exmaple, I'd like to be able to do: File foo.py ...
6
by: Joe HM | last post by:
Hello - The following code will crash if the TestA() instances are declared as Private and work just fine if they are Public. Is there a way to hide these instances to callers from outside the...
4
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be...
7
by: dmitrey | last post by:
hi all, can anyone explain howto get function from module, known by string names? I.e. something like def myfunc(module_string1, func_string2, *args): eval('from ' + module_string1 + 'import...
4
by: Tobiah | last post by:
Can I do: getattr(current_module, 'foo') where 'current_module' is a handle the the one that the code is in? Just like getattr(self, 'foo') in a class, but for the current module...
5
by: Torsten Bronger | last post by:
Hallöchen! How can I get a list with all classes defined in the current module? Thank you! Tschö, Torsten. -- Torsten Bronger, aquisgrana, europa vetus
4
by: Mike | last post by:
I want to do something like the following (let's pretend that this is in file 'driver.py'): #!/bin/env python import sys def foo(): print 'foo'
1
by: Alex | last post by:
Can I get reference to module object of current module (from which the code is currently executed)? I know __import__('filename') should probably do that, but the call contains redundant...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
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...
2
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...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
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...
0
isladogs
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...
3
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...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.