class ClassName( SuperClass ) :
A new name space is created, thus all assignments to local variables go into this new namespace.
When a class definitition ends, a class object is created.
attribute names: __doc__( the fist multil-line string ), __class__(class object), variable, function object
instantiation: __init__(self). How to create an instance: X = MyClass()
Instance Objects:
Attribute references: data attribute and method. MyClass.f is a function object, x.f is a method object.
Data attibutes override method attributes with the same name. Note that clients may add data attributes of their own to an instance object
Method Objects:
They are attribute of Instance Objects. the special thing about methods is that the object is passed as the first argument of the function
x.f() is equivalent to MyClass.f(x)
Inheritance:
Method reference are resolved as : the corresponding class attribute, descending down the chain of base classes.
All mehods in Python are virtual.
class BaseClass:
def toString(self):
return "in BaseClass"def f(self):
print self.toString()
class MyClass(BaseClass):
def toString(self):
return "in MyClass"def fb(self):
BaseClass.f(self)if __name__ == "__main__":
>>in MyClass
x = MyClass()
x.fb()
Two builtin functions: isinstance(obj,class), issubclass(class1,class2)
Multiple Inheritance:
class DerivedClassName(Base,Base2,Base3):
old-style classes’s method reference rule: depth-first, left-right
new-style class( any class which inherite from “object”):http://www.python.org/download/releases/2.3/mro/ .(too complicated for now, leave it for later)
Private Variables:
__spam is replaced with _classname__spam, but “__spam__” won’t.