global vars across modules

M

mamboknave

I need to use global var across files/modules:

# file_1.py
a = 0
def funct_1() :
a = 1 # a is global
print(a)


# file_2.py
from file_1 import *
def main() :
funct_1()
a = 2 # a is local, it's not imported
print(a)

Here above 'a' is not imported from file_1, it's local.
The only way I was able to access the global 'a' is the following:

# file_2.py
from file_1 import *
import file_1
def main() :
funct_1()
file_1.a = 2 # a is the global from file_1
print(file_1.a)


Question:
How can I access to the global 'a' in file_2 without resorting to the whole name 'file_1.a' ?

Thanks!
 
M

mamboknave

..
Answer 1: You can't.

Answer 2: You might want to look at thread local storage
(http://docs.python.org/library/threading.html#threading.local).

Answer 3: Are you sure you really want to do this?

Thanks! Here is what I need to do, perhaps you can give me some hints.

A generic module, used across different independent programs, puts its computing results in a var fairly big, ~50KB.

I need the functions in these programs to access that var. No assignment will be made to that var, it will just be accessed to parse its content.

How can I do? I cannot think of making that var local and 'returning' ~50KB all the times the module is called.

Any hint...?
 
R

Roy Smith

.

Thanks! Here is what I need to do, perhaps you can give me some hints.

A generic module, used across different independent programs, puts its
computing results in a var fairly big, ~50KB.

Why do they do that? Why don't they just return the result?
How can I do? I cannot think of making that var local and 'returning' ~50KB
all the times the module is called.

Why not? If you return a 50 kb string or some other data structure,
you're not actually copying 50 kb of data, you're just returning a
handle to the data object.
 
J

John Nagle

Question:
How can I access to the global 'a' in file_2 without resorting to the whole name 'file_1.a' ?

Actually, it's better to use the fully qualified name "file_1.a".
Using "import *" brings in everything in the other module, which often
results in a name clash.

Just do

import file_1

and, if desired

localnamefora = file_1.a



John Nagle
 
T

Thomas 'PointedEars' Lahn

John said:
Actually, it's better to use the fully qualified name "file_1.a".
Using "import *" brings in everything in the other module, which often
results in a name clash.

Just do

import file_1

and, if desired

localnamefora = file_1.a

I think it is better to write

from file_1 import a as localnamefora

instead, unless `localnamefora' should be the identifier of a
(function-)local variable.

<http://docs.python.org/release/2.7.3/reference/simple_stmts.html#the-
import-statement>
<http://docs.python.org/py3k/reference/simple_stmts.html#the-import-
statement>
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top