Variable scope within a class

Gerrit Holl gerrit at
Tue Feb 3 11:18:24 CET 2004

Hi Graham,

> How do I make a variable available to code *inside* my class in such a way
> that it is available to all defs in the class? In my example below f is not
> accessible within the showVars method.
> class myClass:
>     f = [1,2,3]
>     def showVars ( self ):
>         print f # Error here as f does not exist

Since f is a class variable, it is also an instance variable. You can
either reach it by referring to the class or by referring to the
instance. The former can be done with 'self.__class__.f', the latter by
the simpler 'self.f'. The difference is that, if self.f changes, this is
true for only 1 instance of the class, while all share the same


PrePEP: Builtin path type
Asperger's Syndrome - a personal approach:

More information about the Python-list mailing list