Newbie: Classes

Michael Loomington mloomington at yahoo.ca
Sun Oct 26 21:27:41 EST 2003


This is taken from the tutorial:
>>> class Complex:
...     def __init__(self, realpart, imagpart):
...         self.r = realpart
...         self.i = imagpart

So this is a constructor correct because of the __init__ ?
What is the self mean exactly?  And if I create another def can that def see
my r and i variables and can I call them?
So I can do something like?

def real():
    return r

In java you declare your variables outside of methods that's why I'm a
little confused.

The self just means its a constructor?  So really it has two parameters?

Also, I read somewhere you don't need to specify the parameters?  So how can
you assign r and i to 0 if the user doesn't specify the parameters?

In java if you call the instance of a class it returns a string if you wrote
a toString() method in that class.  How can I do that in python?

Thanks.






More information about the Python-list mailing list