In article <2652842.660.1335123578432.JavaMail.geo-discussion-forums@pbckz3>,
mambokn...@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 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.
> # 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'.
On 4/22/2012 12:39 PM, mambokn...@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.
> On 4/22/2012 21:39, mambokn...@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'.
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.
John Nagle wrote:
> On 4/22/2012 12:39 PM, mambokn...@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
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.