OUTDATED, DO NOT USE: This tutorial was written in 1999 and was based on Python 1.5.2, which today is a really old version. I would not recommend using this tutorial to learn modern Python as the language has changed significantly. This page is left here in it's mostly-original form for historic reference only. Please consult the Python website for current documentation.

Defining a class

  • A class is defined with the class statement. The definition (body) of the class is indented underneath. Usually this consists of def statements, which in this case define member functions.
    class myclass:
       def set( self, n ):
          self.data = n
       def get( self, n ):
          return self.data
    myobject = myclass()
    myobject.set( 7 )
    print myobject.get()  --> 7
  • Objects of a given class are created by calling the class name as if it were a function.
  • Member functions or varables of an object or class are qualified, just like for modules.
  • Within member functions, the first parameter (usually called self) refers to the object itself. It is similar to C++'s this pointer. Object data variables do not have to be predefined, theycan they are created just by assigning to them within the "self" namespace.
  • Constructors and destructors are introduced by defining member functions with the special names __init__() and __del__(), respectively. The constructor may take arguments.
    class myclass:
       def __init__( self, n = 1 ):  # constructor
          self.data = n
       def __del__( self ):  # destructor
          print 'Object being removed'
       def set( self, n ):
          self.data = n
       def get( self, n ):
          return self.data

Class methods and variables

Some object-oriented languages like Smalltalk support class methods and data. A class method/variable is shared among all objects of the class, unlike normal object methods and variables which are private to each instance. Class methods/variables are somewhat supported in Python, but requires some explaination.

  • Class variables are introduced just be assigining to them directly under the class statement, but not within a member function. Class variables are shared among all objects of that class. However, once a member function assigns to them they become object-private variables for that object.
    class myclass2:
       classdata = 12  # define a class variable
       def get( self ):
          print self.classdata
       def set( self, i ):
          self.classdata = i
    a = myclass2()
    b = myclass2()
    print a.get(), b.get()   --> 12 12
    a.set( 77 )
    print a.get(), b.get()   --> 77 12
  • Class variables can be referenced either through the class or the object
       print myclass2.classdata  --> 12
       a = myclass2()
       print a.classdata         --> 12
  • What is really going on is the old LGB rule that we saw when looking at function namespaces. To simulate a true shared class variable you really need to create a global variable within your module.
    _count = 0   # global private variable
    class myclass3:
       def __init__(self):
          global _count
          _count = _count + 1
       def __del__(self):
          global _count
          _count = _count - 1
       def howmany(self):
          return _count
    a = myclass3()
    a.howmany()    --> 1
    b = myclass3()
    a.howmany()    --> 2

Operator overloading

  • It is possible to overload the normal operators, like +, -, *, and so forth. Just define member functions with built-in names. Some of the more common ones are shown in the following sample class,
    class mynumber:
       def __init__(self,value=0):
          self.n = value
       def __add__(self, y):  # x + y
          return self.n + y
       def __sub__(self, y):  # x - y
          return self.n - y
       def __mul__(self, y):  # x * y
          return self.n * y
       def __div__(self, y):  # x / y
          return self.n / y