471,357 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 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 16757
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Fernando Rodriguez | last post: by
reply views Thread by Christian Hudon | last post: by
4 posts views Thread by Hole | last post: by
4 posts views Thread by Tobiah | last post: by
5 posts views Thread by Torsten Bronger | last post: by
4 posts views Thread by Mike | last post: by
1 post views Thread by Alex | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.