469,296 Members | 2,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Reference current module?

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 instead?

Thanks,

Toby

--
Posted via a free Usenet account from http://www.teranews.com

Jun 19 '07 #1
4 7661
Tobiah wrote:
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 instead?
You can try __import__() with __name__::
>>foo = 42
mod = __import__(__name__)
getattr(mod, 'foo')
42

STeVe
Jun 19 '07 #2
Tobiah <to**@tobiah.orgwrites:
Can I do:

getattr(current_module, 'foo')

where 'current_module' is a handle the the one
that the code is in? Just like
The python-list archives
<URL:http://mail.python.org/pipermail/python-list/are your friend.

Google is able to search within a site hierarchy by using search terms
like the following:

site:mail.python.org/pipermail/python-list/ current module reference

Searching there, I immediately find this brief thread:

<URL:http://mail.python.org/pipermail/python-list/2006-August/398319.html>
<URL:http://mail.python.org/pipermail/python-list/2006-August/398321.html>

--
\ "I hope if dogs ever take over the world, and they chose a |
`\ king, they don't just go by size, because I bet there are some |
_o__) Chihuahuas with some good ideas." -- Jack Handey |
Ben Finney
Jun 20 '07 #3
En Tue, 19 Jun 2007 16:34:50 -0300, Steven Bethard
<st************@gmail.comescribió:
Tobiah wrote:
>>
getattr(current_module, 'foo')

where 'current_module' is a handle the the one
that the code is in? Just like

You can try __import__() with __name__::
>>foo = 42
>>mod = __import__(__name__)
>>getattr(mod, 'foo')
42
A simple way would be using sys.modules[__name__].
But I prefer using globals()['foo'] as it is simpler and does not assume
additional requirements (like __name__ still being the same, or the module
still available for importing).

--
Gabriel Genellina

Jun 20 '07 #4
Gabriel Genellina wrote:
En Tue, 19 Jun 2007 16:34:50 -0300, Steven Bethard
<st************@gmail.comescribió:
>Tobiah wrote:
>>>
getattr(current_module, 'foo')

where 'current_module' is a handle the the one
that the code is in? Just like
A simple way would be using sys.modules[__name__].
But I prefer using globals()['foo'] as it is simpler and does not assume
additional requirements (like __name__ still being the same, or the
module still available for importing).

--Gabriel Genellina
In this case I really wanted a reference to
the actual module, so that I could walk down
from there in a sort of object mapping:

def remote_call(call = 'foo.bar.other.thing'):
destination_object = sys.modules[__name__]
for part in call.split('.'):
destination_object = getattr(destination_object, part)

Tobiah

--
Posted via a free Usenet account from http://www.teranews.com

Jun 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Ron Adam | last post: by
2 posts views Thread by Stefan Seefeld | last post: by
18 posts views Thread by klawiter | last post: by
4 posts views Thread by Keith Chadwick | last post: by
3 posts views Thread by Don | last post: by
reply views Thread by punjabinezzie | last post: by
8 posts views Thread by chamalulu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.