How to define a GLOBAL Variable ?

Steve Holden sholden at holdenweb.com
Thu Aug 2 18:02:25 CEST 2001


"François Pinard" <pinard at iro.umontreal.ca> wrote in message
news:mailman.996764823.3747.python-list at python.org...
> [Peter Moscatt]
>
> > If I wish to define a variable that I want to become global to ALL
classes
> > within a .py file - how is this best achieved ?
>
> Hell, Peter.
>
> It's easy (like most things in Python!).  Just define your class global
> variable, using the class name to the left of a dot.  For example:
>
>         >>> class Factory:
>         ... def printer(self):
>         ... print self.value
>         ...
>         >>> a = Factory()
>         >>> b = Factory()
>         >>> Factory.value = 3.1415926
>         >>> a.printer()
>         3.1415926
>         >>> b.printer()
>         3.1415926
>
Unfortunately this makes the value attribute globl to all instances of a
class, rather than all classes of the module as requested.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list