When defining your class methods, you *must* explicitly list self as the
first argument for each method, including __init__. When you call a method
of an ancestor class from within your class, you *must* include the
selfargument. But when you call your class method from outside, you do
not
specify anything for the self argument; you skip it entirely, and
Pythonautomatically adds the instance reference for you. I am aware
that this is
confusing at first; it's not really inconsistent, but it may appear
inconsistent because it relies on a distinction (between bound and unbound
methods) that you don't know about yet.

So, you have to do:

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

2009/7/18 Ronn Ross <ronn.r...@gmail.com>

> 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?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to