473,231 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 8054
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ron Adam | last post by:
Does anyone have suggestions on how to improve this further? Cheers, Ron_Adam def getobjs(object, dlist=, lvl=0, maxlevel=1): """ Retrieve a list of sub objects from an object. """
2
by: Stefan Seefeld | last post by:
hello, I'v run into a bug that I find hard to understand: In a python module of mine I import system modules ('sys', say) and then use them from within some functions. However, during...
18
by: klawiter | last post by:
Greetings, I would like to change the background color of fields as they are entered for editing. (then, of course, change them back on exit) My prototype consists of using a couple of global...
4
by: Keith Chadwick | last post by:
I am having some trouble referencing an Application("myVar") variable from within a module.vb file on my ASP.NET site. According to the documentation I should be able to reference...
3
by: Don | last post by:
I have a module where I have common routines. When I want to use it I place the following on top of the sompage.aspx.v imports Mycode.CWPS.CWPSCod I then call a routine that I want to use. ...
0
by: Bahige | last post by:
I already have a working Visual Basic .Net Add-in. It loads when the IDE loads and there is a button to run it from the tools menu. It also creates a tool window that can be docked anywhere on...
0
by: punjabinezzie | last post by:
I am developing an Application which currently has two XML files. One XML file with some nodes and an external entity reference to another XML file. The source XML file is being parsed using Xerces...
2
by: RLN | last post by:
I have this error message format I use for my error handling in all modules, both in vba code inside of a form and vba code that is outside of forms: MsgBox Err.Number & "-" & Err.Description,...
8
by: chamalulu | last post by:
Hello. I think I'm aware of how attribute access is resolved in python. When referencing a class instance attribute which is not defined in the scope of the instance, Python looks for a class...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.