Beginners question

gabrielmonnerat gabrielmonnerat at gmail.com
Fri Jul 17 22:03:32 EDT 2009


Ronn Ross wrote:
> How do you define a global variable in a class. I tried this with do 
> success:
> class ClassName:
>     global_var = 1
>    
>     def some_methos():
>         print global_var
>
> This doesn't work. What am I doing wrong?
You need pass "self" to the function and "global_var" needs be called 
with self.

class ClassName:
    global_var = 1
   
    def some_methos(self):
        print self.global_var


Gabriel M. Monnerat



More information about the Python-list mailing list