Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
global vars across modules
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mambokn...@gmail.com  
View profile  
 More options Apr 22 2012, 3:39 pm
Newsgroups: comp.lang.python
From: mambokn...@gmail.com
Date: Sun, 22 Apr 2012 12:39:38 -0700 (PDT)
Local: Sun, Apr 22 2012 3:39 pm
Subject: global vars across modules
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!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Apr 22 2012, 3:48 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sun, 22 Apr 2012 15:48:23 -0400
Local: Sun, Apr 22 2012 3:48 pm
Subject: Re: global vars across modules
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 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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mambokn...@gmail.com  
View profile  
 More options Apr 22 2012, 4:08 pm
Newsgroups: comp.lang.python
From: mambokn...@gmail.com
Date: Sun, 22 Apr 2012 13:08:05 -0700 (PDT)
Local: Sun, Apr 22 2012 4:08 pm
Subject: Re: global vars across modules
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...?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Apr 22 2012, 4:53 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sun, 22 Apr 2012 16:53:18 -0400
Local: Sun, Apr 22 2012 4:53 pm
Subject: Re: global vars across modules
In article
<11146533.5.1335125285850.JavaMail.geo-discussion-forums@pboo1>,

 mambokn...@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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kiuhnm  
View profile  
 More options Apr 22 2012, 5:08 pm
Newsgroups: comp.lang.python
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Date: Sun, 22 Apr 2012 23:08:29 +0200
Local: Sun, Apr 22 2012 5:08 pm
Subject: Re: global vars across modules
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'.

Kiuhnm


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Nagle  
View profile  
 More options Apr 22 2012, 5:38 pm
Newsgroups: comp.lang.python
From: John Nagle <na...@animats.com>
Date: Sun, 22 Apr 2012 14:38:28 -0700
Local: Sun, Apr 22 2012 5:38 pm
Subject: Re: global vars across modules
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

                                        John Nagle


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kiuhnm  
View profile  
 More options Apr 22 2012, 6:13 pm
Newsgroups: comp.lang.python
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Date: Mon, 23 Apr 2012 00:13:19 +0200
Local: Sun, Apr 22 2012 6:13 pm
Subject: Re: global vars across modules
On 4/22/2012 23:08, Kiuhnm wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mambokn...@gmail.com  
View profile  
 More options Apr 22 2012, 6:46 pm
Newsgroups: comp.lang.python
From: mambokn...@gmail.com
Date: Sun, 22 Apr 2012 15:46:02 -0700 (PDT)
Local: Sun, Apr 22 2012 6:46 pm
Subject: Re: global vars across modules
Thanks a lot to everyone! This stuff is clear now.

All the best.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Apr 25 2012, 4:13 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Wed, 25 Apr 2012 10:13:49 +0200
Local: Wed, Apr 25 2012 4:13 am
Subject: Re: global vars across modules

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »