473,387 Members | 3,821 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,387 software developers and data experts.

accessing module global vars by name

Withing a module I can assign a value to a global var by assigning to
it in the outermost scope. Fine.

But how can I do this if the attribute name itself is kept in a
variable. Once the module is loaded I can access the module's
namespace no problem, but inside the module the dictionary is not yet
present right ?

IOW how can I write something like

# xxx.py

for varName in ("foo", "bar"):
magic.varName = 1

so I can later refer to them as

# yyy.py
import xxx
x = xxx.foo
y = xxx.bar
Jul 18 '05 #1
2 1611
Martin Drautzburg wrote:
IOW how can I write something like

# xxx.py

for varName in ("foo", "bar"):
magic.varName = 1


I think you want to use the dict returned by globals(). Modifying this
dict can add/remove names from the global scope.[1]
foo Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'foo' is not defined bar Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'bar' is not defined for var_name in ['foo', 'bar']: .... globals()[var_name] = True
.... foo True bar True del globals()['foo']
foo

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'foo' is not defined

Steve

[1] As an aside, be careful not to try the same thing with locals().
locals() returns a dict that won't modify names in the local scope.
Jul 18 '05 #2
Martin Drautzburg wrote:
Withing a module I can assign a value to a global var by assigning to
it in the outermost scope. Fine.

But how can I do this if the attribute name itself is kept in a
variable. Once the module is loaded I can access the module's
namespace no problem, but inside the module the dictionary is not yet
present right ?


Look into the builtin function globals()...

-Peter
Jul 18 '05 #3

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

Similar topics

14
by: lkrubner | last post by:
If I set a variable at the top of my code like this: $name = "Lawrence"; It is now a global variable. If, later on, in a function, I want to do this: function uppercaseName() { global...
3
by: Tonguç Yumruk | last post by:
I'm trying to build a completely plug-in based system. One of my problems is importing a package dynamically. I'm trying to emulate the import command. The __import__() function or imp module...
0
by: Kepes Krisztian | last post by:
Hi ! How to access my module global vars ? Some applications I want to declare global vars (constants). But in the module I cannot access them: DEF_X=120
6
by: Fernando Rodríguez | last post by:
Hi, I haven't used Python in quite some time, and I'm bit puzzled by this: counter = 0 class Blah(object): def run(self): counter += 1
14
by: James Thiele | last post by:
I'd like to access the name of a function from inside the function. My first idea didn't work. >>> def foo(): .... print func_name .... >>> foo() Traceback (most recent call last): File...
1
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the...
3
by: Peter J. Bismuti | last post by:
How do you define a "module data member" (I want to understand out how this works before making converting to a Class)? Right now I'm defining variables in a module that get put into the global...
0
by: hadrien | last post by:
Hi! I'm working with Python and MySQL and I need two global vars for an entire module. These two vars would be the connector and the cursor of the current database and I need them to work...
0
by: norseman | last post by:
mercado mercado wrote: =================================== I started to import a module using its path and now see what you mean. Python is missing the concept: Programmer dictates what machine...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.