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++
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 94
July 17, 2000, beazley@cs.uchicago.edu
>>>