From: James Mills on
On Mon, May 17, 2010 at 3:50 AM, AON LAZIO <aonlazio(a)gmail.com> wrote:
> Hi,
>    How can I set up global variables for the entire python applications?
> Like I can call and set this variables in any .py files.
>    Think of it as a global variable in a single .py file but this is for the
> entire application.

If you have to use global variables in your application you are
designing it WRONG!

Python has powerful support for object orientated programming. Use it!

I highly recommend the Python tutorial - especially the section on classes.

--James
From: Krister Svanlund on
On Sun, May 16, 2010 at 7:50 PM, AON LAZIO <aonlazio(a)gmail.com> wrote:
> Hi,
>    How can I set up global variables for the entire python applications?
> Like I can call and set this variables in any .py files.
>    Think of it as a global variable in a single .py file but this is for the
> entire application.
>    Thanks
>
> --
> Aonlazio
> 'Peace is always the way.' NW

First: Do NOT use global variables, it is bad practice and will
eventually give you loads of s**t.

But if you want to create global variables in python I do believe it
is possible to specify them in a .py file and then simply import it as
a module in your application. If you change one value in a module the
change will be available in all places you imported that module in.
From: Chris Rebert on
On Sun, May 16, 2010 at 10:50 AM, AON LAZIO <aonlazio(a)gmail.com> wrote:
> Hi,
>    How can I set up global variables for the entire python applications?
> Like I can call and set this variables in any .py files.
>    Think of it as a global variable in a single .py file but this is for the
> entire application.

Thankfully, there is no such thing (can you say spaghetti code?). The
closest approximation, as I said in my previous reply, is to use the
namespace of a designated module for this purpose, and import that
module wherever you need to access/modify these "superglobal"
variables.

Example:
#g.py:
#this module exists to hold superglobal vars
global1 = "foo"
global2 = "bar"


#elsewhere.py:
#this is some other module in the same program
import mypackage.g as g

print "global #1 = ", g.global1
print "global #2 =", g.global2
g.global1 = "baz" # modify a superglobal
g.global3 = "qux" # create a new superglobal


Cheers,
Chris
--
http://blog.rebertia.com
From: James Mills on
On Mon, May 17, 2010 at 4:00 AM, Krister Svanlund
<krister.svanlund(a)gmail.com> wrote:
> On Sun, May 16, 2010 at 7:50 PM, AON LAZIO <aonlazio(a)gmail.com> wrote:
>>    How can I set up global variables for the entire python applications?
>> Like I can call and set this variables in any .py files.
>>    Think of it as a global variable in a single .py file but this is for the
>> entire application.
>
> First: Do NOT use global variables, it is bad practice and will
> eventually give you loads of s**t.
>
> But if you want to create global variables in python I do believe it
> is possible to specify them in a .py file and then simply import it as
> a module in your application. If you change one value in a module the
> change will be available in all places you imported that module in.

The only place global variables are considered somewhat "acceptable"
are as constants in a module shared as a static value.

Anything else should be an object that you share. Don't get into the
habit of using global variables!

--james
From: christian schulze on
On 16 Mai, 20:20, James Mills <prolo...(a)shortcircuit.net.au> wrote:
> On Mon, May 17, 2010 at 4:00 AM, Krister Svanlund
>
> <krister.svanl...(a)gmail.com> wrote:
> > On Sun, May 16, 2010 at 7:50 PM, AON LAZIO <aonla...(a)gmail.com> wrote:
> >>    How can I set up global variables for the entire python applications?
> >> Like I can call and set this variables in any .py files.
> >>    Think of it as a global variable in a single .py file but this is for the
> >> entire application.
>
> > First: Do NOT use global variables, it is bad practice and will
> > eventually give you loads of s**t.
>
> > But if you want to create global variables in python I do believe it
> > is possible to specify them in a .py file and then simply import it as
> > a module in your application. If you change one value in a module the
> > change will be available in all places you imported that module in.
>
> The only place global variables are considered somewhat "acceptable"
> are as constants in a module shared as a static value.
>
> Anything else should be an object that you share. Don't get into the
> habit of using global variables!
>
> --james

Exactly! Python's OOP is awesome. Use it. Global vars used as anything
but constants is bad practice. It isn't that much work to implement
that.