More on Class Members
Methods
class Account:
...
def deposit(self,amt):
self.balance = self.balance + amt
First argument to a method always refers to a specific instance (self)
Operations on an object must explicitly refer to the self parameter
self.balance = self.balance + amt # This updates the object
balance = balance + amt # This is name error (balance unknown)
For the C++ impaired, self is like the 'this' variable.
Variables
class Foo:
a = 42
Variable is shared by all instances
Like a static member variable in C++
|