473,405 Members | 2,160 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.

passing globals to imported module


Had a question from a colleague that I embarrassingly couldn't answer.

He has a script, foo.py with a global. He wants to import bar.py and
needs that global available in bar.py

The following obviously doesn't work:

# foo.py
my_global = "hello"
print globals().keys()
import bar
# bar.py
print globals().keys()
and results in:

['__builtins__', '__name__', '__doc__', 'my_global']
['__builtins__', '__name__', '__file__', '__doc__']
I'm not sure how to reimplement __import__ to make the global available
to the imported module.

Any suggestions?

James
--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/

Jul 18 '05 #1
5 1543
James Tauber wrote:

Had a question from a colleague that I embarrassingly couldn't
answer.

He has a script, foo.py with a global. He wants to import bar.py
and needs that global available in bar.py
Note that "global" in python means global in the current module.
The following obviously doesn't work:

# foo.py
my_global = "hello"
print globals().keys()
import bar
# bar.py
print globals().keys()


If you need the global _after_ the import you can just add in foo.py:

bar.my_global = my_global

Another solution would be to use a better design;)

Mathias
Jul 18 '05 #2

On Mon, 16 Aug 2004 21:24:16 +0200, "Mathias Waack" <M.*****@gmx.de>
said:
If you need the global _after_ the import you can just add in foo.py:

bar.my_global = my_global

Another solution would be to use a better design;)


Well, changing the design was my first thought too :-)

I just wondered whether there was a way to set the globals for the
import module *before* top level code in the improted module is
executed. I'm unclear what the second arg to __import__ actually
affects.

James
--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/

Jul 18 '05 #3
James Tauber wrote:
Had a question from a colleague that I embarrassingly couldn't answer.

He has a script, foo.py with a global. He wants to import bar.py and
needs that global available in bar.py


#### global.py
my_global = "Hello!"
#### foo.py
import global, bar

print global.my_global
#### bar.py
import global

print global.my_global

;)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #4
James Tauber wrote:
He has a script, foo.py with a global. He wants to import bar.py and
needs that global available in bar.py


He should _not_ consider the following hack:
import new, sys
bar = new.module("bar")
bar.my_global = "some value"
sys.modules["bar"] = bar
execfile("bar.py", bar.__dict__) bar
['__builtins__', '__name__', '__doc__', 'my_global']
some value


$ cat bar.py
print __name__
print globals().keys()

def demo():
print my_global

demo()

Peter

Jul 18 '05 #5

Actually he liked that a lot.

I think it's elegant too, although my preference is still for him to
change his design.

But thanks!

On Mon, 16 Aug 2004 13:09:49 -0700, "Jeff Shannon" <je**@ccvcorp.com>
said:
James Tauber wrote:
Had a question from a colleague that I embarrassingly couldn't answer.

He has a script, foo.py with a global. He wants to import bar.py and
needs that global available in bar.py


#### global.py
my_global = "Hello!"
#### foo.py
import global, bar

print global.my_global
#### bar.py
import global

print global.my_global

;)

Jeff Shannon
Technician/Programmer
Credit International

--
http://mail.python.org/mailman/listinfo/python-list

--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/

Jul 18 '05 #6

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

Similar topics

2
by: Robin Munn | last post by:
OK, here's another software design question, one that's been bugging me for a while now. What do y'all think is the best way to handle program-level globals, such as configuration option --...
4
by: Chris | last post by:
I'm trying to come up with a not-so-ugly manner of passing many command-line options between modules. I have a Steering.py file, which contains my main() and handles the getopts getting of...
6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
18
by: fred.dixon | last post by:
i have read the book and searched the group too ---------------------------------------------- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2...
1
by: Harold Fellermann | last post by:
Dear list, I looked through the list but could not find any solutions for my current problem. Within my program, I am importing a module via __import__(module_name,globals(),locals()) and I...
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
2
by: HMS Surprise | last post by:
In the file snippet below the value for the global hostName is determined at runtime. Functions imported from the parent baseClass file such as logon also need access to this variable but cannot...
20
by: MartinRinehart | last post by:
Is the following correct? x = "some string" x is a reference to "some string" foo(x) Reference is passed to function.
3
by: r0g | last post by:
Hi There, I'm refactoring some old code that uses global variables and was originally written in one big flat file with a view to nicening it up and then extending it. The problem I have though...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.