Beginners question

nohics nohics nohics at gmail.com
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
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.ross at 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090718/472c1523/attachment.html>


More information about the Python-list mailing list