Constants and Globals
Mike Meyer
mwm at mired.org
Sat Dec 14 10:27:30 EST 2002
More information about the Python-list mailing list
Sat Dec 14 10:27:30 EST 2002
- Previous message (by thread): Constants and Globals
- Next message (by thread): Constants and Globals
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Travis Beaty <t.beaty at mchsi.com> writes: > 1. Is there such an animal as a "constant" in Python? I've looked > through the manual, documentation, and FAQ's, but I haven't been able > to find an answer about that. I'm sure it's there somewhere, and I've > just overlooked it. I noted that the term "constant" was used, but I > am unsure whether one can actually lock the value of a variable, or > whether such constants are simply "constant by agreement." You appear to be suffering from a standard newbie confusion about variables. Variables aren't objects, they refer to them. Assignment causes the right hand variable to refer to the object created by the expression on the left hand side of the '='. You can have a variable refer to a constant. What you can't do is tie a variable to an object so that it can't be assigned to. So yes, there are constants. You can make variables refer to them. You can't tell the interpreter to disallow assignments to a variable. So there aren't constants the way you mean it. > 2. I'm working on an application that needs to have a global > variable. I, of course, do my best to shy away from globals; however, > since this is a dictionary which contains user preference settings, I > can see no other alternative. I've implemented this global variable > by placing it into a module which will be imported by all other > modules. Is this the "standard" way that globals are created? Again, > I've read the docs and tutorial where variable scope is concerned, but > I'm still a little unclear on it. There are no global variables. You found the standard way for sharing data across modules - put it in a module. Better yet, create a class that has all the preferences settings in it, and make that a module global. You can then save settings by pickling that object to a file, and reload them by unpickling the object. <mike -- Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
- Previous message (by thread): Constants and Globals
- Next message (by thread): Constants and Globals
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list