You seem to be getting the hang of it. The only one that you were completely wrong about is z = 300
. This is a name that is local to the __init__
method. Python never inserts self
for you in the same manner that C++ and Java will assume this
where it can.
One thing to remember as you continue learning Python is that member functions can always be executed as class members. Consider the following:
>>> class Sample(object):
... def __init__(self, value):
... self.value = value
... def get_value(self):
... return self.value
...
>>> s = Sample(1)
>>> t = Sample(2)
>>> s.get_value()
1
>>> Sample.get_value(s)
1
>>> t.__class__.get_value(s)
1
The last three examples all call the member function of the s
object. The last two use the fact that get_value
is just an attribute of the Sample
class that expects to receive an instance of Sample
as the argument.