Beginners question

nohics nohics nohics at
Sat Jul 18 04:40:32 CEST 2009

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
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.ross at>

> 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?
> --
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list