passing globals to imported module

J

James Tauber

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
 
M

Mathias Waack

James said:
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
 
J

James Tauber

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
 
J

Jeff Shannon

James said:
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
 
P

Peter Otten

James said:
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
 
J

James Tauber

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top