Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

global vars across modules

57 views
Skip to first unread message

mambo...@gmail.com

unread,
Apr 22, 2012, 3:39:38 PM4/22/12
to
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!

Roy Smith

unread,
Apr 22, 2012, 3:48:23 PM4/22/12
to
In article
<2652842.660.1335123578432.JavaMail.geo-discussion-forums@pbckz3>,
mambo...@gmail.com wrote:

> I need to use global var across files/modules:
[...]
> Question:
> How can I access to the global 'a' in file_2 without resorting to the whole
> name 'file_1.a' ?

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?

mambo...@gmail.com

unread,
Apr 22, 2012, 4:08:05 PM4/22/12
to
On Sunday, April 22, 2012 12:48:23 PM UTC-7, Roy Smith wrote:
....
> 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...?

Roy Smith

unread,
Apr 22, 2012, 4:53:18 PM4/22/12
to
In article
<11146533.5.1335125285850.JavaMail.geo-discussion-forums@pboo1>,
mambo...@gmail.com wrote:

> On Sunday, April 22, 2012 12:48:23 PM UTC-7, Roy Smith wrote:
> ....
> > 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.

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.

Kiuhnm

unread,
Apr 22, 2012, 5:08:29 PM4/22/12
to
On 4/22/2012 21:39, mambo...@gmail.com wrote:
> 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)

You just forgot to declare 'a' as global inside your functions.

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


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

When you write 'a = 1' and 'a = 2' you create local variables named 'a'
local to your functions, unless you specify that you're referring to a
global variable by declaring 'a' as 'global'.

Kiuhnm

John Nagle

unread,
Apr 22, 2012, 5:38:28 PM4/22/12
to
On 4/22/2012 12:39 PM, mambo...@gmail.com wrote:

>
> 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

Kiuhnm

unread,
Apr 22, 2012, 6:13:19 PM4/22/12
to
Actually, that doesn't work. I never used "external variables" in Python
and I thought "from file_1 import *" worked differently. What a
surprise. It makes sense though.
"Import" imports values, not variables.

Anyway, you don't need all this.

Kiuhnm

mambo...@gmail.com

unread,
Apr 22, 2012, 6:46:02 PM4/22/12
to
Thanks a lot to everyone! This stuff is clear now.

All the best.

Thomas 'PointedEars' Lahn

unread,
Apr 25, 2012, 4:13:49 AM4/25/12
to
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>

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.
0 new messages