niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
global variables
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
Author Message
Steve "TZOTZIOY"
*nix forums beginner


Joined: 04 Feb 2005
Posts: 1

PostPosted: Fri Feb 04, 2005 11:07 pm    Post subject: Re: [OT] PEP 8j, pythonista naming style Reply with quote

On Fri, 04 Feb 2005 23:56:44 +1000, rumours say that Steve Coghlan
<scoghlan@iinet.net.au> might have written:

Quote:
A Steve wrote:
A Steve wrote:

A Steve wrote:


There we go, much clearer Wink

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?
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Back to top
Nick Coghlan
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 131

PostPosted: Fri Feb 04, 2005 12:56 pm    Post subject: Re: global variables Reply with quote

A Steve wrote:
Quote:
A Steve wrote:

A Steve wrote:


There we go, much clearer ;)

Cheers,
Nick.

--
Nick Coghlan | ncoghlan@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Back to top
Steve Holden
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1255

PostPosted: Thu Feb 03, 2005 5:12 pm    Post subject: Re: global variables Reply with quote

Steven Bethard wrote:

Quote:
Steve Holden wrote:

M.E.Farmer wrote:

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


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

STeve

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

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

Stevbe - see?
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Back to top
Steven Bethard
*nix forums Guru


Joined: 20 Feb 2005
Posts: 773

PostPosted: Thu Feb 03, 2005 4:41 pm    Post subject: Re: global variables Reply with quote

Steve Holden wrote:
Quote:
M.E.Farmer wrote:

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

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

STeve

[1] But of course, you probably do the same thing too. Wink
Back to top
Michael
*nix forums Guru


Joined: 28 Mar 2003
Posts: 352

PostPosted: Thu Feb 03, 2005 4:35 pm    Post subject: Re: global variables Reply with quote

Quote:
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.'
***

--
Michael <mogmios@mlug.missouri.edu>
http://kavlon.org
Back to top
Mark Jackson
*nix forums beginner


Joined: 11 Jul 2005
Posts: 10

PostPosted: Thu Feb 03, 2005 12:36 pm    Post subject: Re: global variables Reply with quote

Steve Holden <steve@holdenweb.com> writes:
Quote:
M.E.Farmer wrote:

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

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

--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
People who write obscurely are either unskilled in writing
or up to mischief. - Sir Peter Medawar
Back to top
Steve Holden
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1255

PostPosted: Thu Feb 03, 2005 12:06 pm    Post subject: Re: global variables Reply with quote

M.E.Farmer wrote:

Quote:
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
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Back to top
M.E.Farmer
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 105

PostPosted: Thu Feb 03, 2005 12:04 am    Post subject: Re: global variables Reply with quote

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

M.E.Farmer
Back to top
M.E.Farmer
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 105

PostPosted: Wed Feb 02, 2005 11:42 pm    Post subject: Re: global variables Reply with quote

Steve,
Yes I agree Wink 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

Steven Bethard wrote:
Quote:
M.E.Farmer wrote:
alex wrote:
is it possible to create 'global' variables that can be seen in
all
other classes?

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
Back to top
Larry Bates
*nix forums Guru


Joined: 21 Feb 2005
Posts: 422

PostPosted: Wed Feb 02, 2005 11:26 pm    Post subject: Re: global variables Reply with quote

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

alex wrote:
Quote:
Hi,

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

Alex
Back to top
Steven Bethard
*nix forums Guru


Joined: 20 Feb 2005
Posts: 773

PostPosted: Wed Feb 02, 2005 11:23 pm    Post subject: Re: global variables Reply with quote

M.E.Farmer wrote:
Quote:
alex wrote:
is it possible to create 'global' variables that can be seen in all
other classes?

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
Back to top
M.E.Farmer
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 105

PostPosted: Wed Feb 02, 2005 11:01 pm    Post subject: Re: global variables Reply with quote

alex wrote:
Quote:
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
Back to top
Steve Holden
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1255

PostPosted: Wed Feb 02, 2005 10:26 pm    Post subject: Re: global variables Reply with quote

alex wrote:

Quote:
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
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Back to top
alex
*nix forums beginner


Joined: 02 Feb 2005
Posts: 1

PostPosted: Wed Feb 02, 2005 10:04 pm    Post subject: global variables Reply with quote

Hi,

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

Alex
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 12:48 am | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Quoting, variables, and xterm graftonfot@yahoo.com shell 2 Thu Jul 20, 2006 2:26 pm
No new posts Question on comparing to variables containing integers huub Perl 14 Thu Jul 20, 2006 11:02 am
No new posts Interpret variables in strings in ksh greenhakan@gmail.com shell 2 Wed Jul 19, 2006 2:03 pm
No new posts Retrieve ext. variables in python program alfa1234 python 7 Wed Jul 19, 2006 7:59 am
No new posts How to add variables to strings? yusufm Perl 6 Tue Jul 18, 2006 7:02 pm

Learn real Kung Fu | 0 Credit Cards | Remortgages | Mortgage | Debt Consolidation
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 2.6108s ][ Queries: 20 (2.4925s) ][ GZIP on - Debug on ]