473,405 Members | 2,176 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,405 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 8067
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
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...
0
isladogs
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...

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.