Object-Oriented Programming

Features supported

  • Multiple inheritance
  • Method overriding (all methods are virtual)

Features not supported

  • Private and protected members (all methods and data attributes are public)
  • Static members (class members) (all methods and data attributes belong to individual objects)
  • Non-virtual methods (final methods)

Quirky features

  • Child data attributes will override parent data attributes of the same name.
  • Data attributes can override methods (consistent naming conventions are needed to avoid confusion of this sort).

Misc

  • A method can call the method of a base class with the same name: BaseClassName.methodName(self, args).  This only works if the base class is defined or imported directly in the global scope.

Defining a class

class ClassName [(BaseClass [,BaseClass]*)] :
dataName = value
def methodName(self [, arg]*):
suite
def __standardMethodName__(self [, arg]*):
suite
  • __init__  # constructor (initializor method)
  • __del__  # destructor (cleanup method)
class ParentClass:
def __init__(self, x):
self.x = x
def __del__(self):
del self.x
def foo(self):
pass

class ChildClass(ParentClass):
def __init__(self, x, y):
ParentClass.__init__(self, x)
self.y = y
def __del__(self):
ParentClass.__del__(self)
del self.y
def foo(self):
ParentClass.foo(self)

Creating an instance of a class

instanceName = ClassName( [arg [,arg]*] )
x = MyClass()
y = ChildClass(123, 456)

Disposing of an instance of a class

del instanceName

Related functions


hasattr( object, name )
  • Returns true if object has an attribute called name.
getattr( object, name [, default] )
  • Gets the named attribute from object.
  • If not found raises AttributeError exception, or returns default if specified.
setattr( object, name, value )
  • Sets the named attribute in object.
  • Creates the attribute if it doesn't exist.
setattr(childClass, 'y', 789)

if hasattr(childClass, 'y'):
print getattr(childClass, 'y')