global variables

A

alex

Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex
 
S

Steve Holden

alex said:
Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex
Not sensibly, though you can mess around with the __builtin__ namespace
to make values accessible without qualification.

The usual solution is to maintain a config module that establishes
default settings for configuration variables. Other modules that import
config can access (and change) those values using

config.name = value

and so on. Hope this help.

regards
Steve
 
M

M.E.Farmer

alex said:
Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex
Hello,
What about using a class?

Py> class globalVar:
.... pass

Py> globals = globalVar()

Now you can assign 'variables' to it.
And use it anywhere you need it.

Py> globals.image_height = (255,777)
Py> globals.image_mode = 'RGB'
Py> globals.image_names = ['this.jpg', that.jpg']
etc...
hth,
M.E.Farmer
 
S

Steven Bethard

M.E.Farmer said:
What about using a class?

Py> class globalVar:
... pass

Py> globals = globalVar()

Probably naming it something other than 'globals' would be a good idea
-- otherwise you'll hide the builtin globals() function.

But I agree that the attributes of a class instance (as you suggest) or
the attributes of a module (as Steve Holden suggests) is probably the
right way to go.

Steve
 
L

Larry Bates

One way to to this is by using keyword args:

class a:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.arg1=arg1
self.arg2=arg2
self.__dict__.update(kwargs)
return

class b:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.update(kwargs)
self.a=a(arg1, arg2, **kwargs)
return

class c:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.update(kwargs)
self.b=b(arg1, arg2, **kwargs)
return

globals={'global1':1, 'global2':2, 'global3':3, 'global4':4}
C=c(1, 2, **globals)

you will have global1, global2, global3, and global4 attributs
in all classes. If you don't want the attributes, just access
to the values, delete the self.__dict__.update(kwargs) lines.

Larry Bates
 
M

M.E.Farmer

Steve,
Yes I agree ;) Never use builtin names.
I know better but missed it somehow.
I apologize for any confusion I may have caused.
Thank you Steve for the correction.
M.E.Farmer
 
M

M.E.Farmer

Ok it has been a long day,
In my reply to Steven Bethard , Steve should read Steven ;)

M.E.Farmer
 
S

Steve Holden

M.E.Farmer said:
Ok it has been a long day,
In my reply to Steven Bethard , Steve should read Steven ;)

M.E.Farmer
Well, since he signs himself "Steve" too I guess we'll just have to put
up with the ambiguities. Or perhaps, given my (lack of) typing skill, I
should just start signing myself "Stvev"?

regards
Steve
 
M

Mark Jackson

Steve Holden said:
Well, since he signs himself "Steve" too I guess we'll just have to put
up with the ambiguities. Or perhaps, given my (lack of) typing skill, I
should just start signing myself "Stvev"?

What's this doing *here*? I thought the discussion of the pitfalls of
name rebinding was taking place in the "variable declaration" thread.
 
M

Michael

Probably naming it something other than 'globals' would be a good idea
-- otherwise you'll hide the builtin globals() function.
But I agree that the attributes of a class instance (as you suggest)
or the attributes of a module (as Steve Holden suggests) is probably
the right way to go.

I like to use 'runtime' or 'runtime_options' to store the results of
command-line options. I wasn't sure about this way of getting Python to
handle global variables when I first tried it but after a little
experience with it I think it works out pretty well. One thing I did
learn though is that it's best to keep these modules simple. Don't make
them import or define classes or functions if you can avoid it. It's
easy to get into a mess of recursive imports if you start doing that.
Just a good newbie tip.

***
if runtime.verbose: print 'Something happened.'
***
 
S

Steven Bethard

Steve said:
Well, since he signs himself "Steve" too I guess we'll just have to put
up with the ambiguities. Or perhaps, given my (lack of) typing skill, I
should just start signing myself "Stvev"?

regards
Steve

Or I can stop correcting it every time I type STeve[1]. ;)

STeve

[1] But of course, you probably do the same thing too. ;)
 
S

Steve Holden

Steven said:
Steve said:
Well, since he signs himself "Steve" too I guess we'll just have to
put up with the ambiguities. Or perhaps, given my (lack of) typing
skill, I should just start signing myself "Stvev"?

regards
Steve


Or I can stop correcting it every time I type STeve[1]. ;)

STeve

[1] But of course, you probably do the same thing too. ;)

Indeed. I defy anyone to be a crappier typist then me.

Stevbe - see?
 
S

Steve TZOTZIOY Georgiou

There we go, much clearer ;)

Indeed. I recall some Dan Perl who was advised to change his name to a more
pythonic one, but now I see he misinterpreted the advice.

Am I assimilated or what?
 

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

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top