Use of variables form outside the class.

Ron Klatchko ron at library.ucsf.edu
Thu Jul 29 12:36:20 EDT 1999


> > How do I reach the "x" variable that is outside any class from inside the
> > class functions?
> 
> x = 1
> 
> class X
>    def set_x(self):
>       self.x = 2
> 
>    def set_global_x(self):
>       global x
>       x = 2

   def get_x(self):
       return self.x

   def get_global_x(self):
       return x

Also, global is only necessary if you want to modify the variable.  If
you only need to access it, the Python namespace rules will find it
without needing the global statement.

moo
----------------------------------------------------------------------
          Ron Klatchko - Manager, Advanced Technology Group           
           UCSF Library and Center for Knowledge Management           
                        ron at library.ucsf.edu




More information about the Python-list mailing list