See All Titles |
![]() ![]() InheritanceA subclass is a class that inherits attribute names and methods from another class—the operation is called subclassing. A base class (superclass) is defined as a class that another class inherits attributes from. Base classes are listed in parentheses in a subclass header. You have to separate base classes by putting commas between them, within the parentheses. When you create a subclass, you can add or overwrite any methods of its base classes. Python classes can be created:
For a conceptual standpoint, take a look at the following example Where,
The subsequent class defines a complex class called Employee. class Employee: def __init__(self,name,salary=0): self.name = name self.salary = salary self.family = [] def raisesalary(self, percent): self.salary = self.salary + (self.salary * percent) def work (self): print self.name, "writes computer code." def hasfamily(self): return len(self.family) == 0 # returns a boolean result def addmember(self, x): self.family.append(x) def removemember(self, x): if len(self.family) > 0: x = self.family[-1] del self.family[-1] return x The next class is a subclass of the Employee class. class Person(Employee): "this is the class Person" def __init__ (self, name): Employee.__init__ (self, name, 50000) def work (self): print self.name, "works like any other employee." Inherited methods of base classes aren't automatically called. It is necessary to call them explicitly. That's why, in the previous example, the Person.__init__ method had to call the Employee.__init__ method. It is always necessary to pass the self argument because base classes don't know what instance is being used. The previous example passes three parameters to the base class's __init__ method (the self reference, an argument, and a default value for the other argument). Multiple inheritance is defined by entering multiple classes in the header of a new class. The order used for informing the base classes really does matter. The precedence order, for a search in the base classes, starts at the classes located at the left side. class A: pass class B(A): pass class C: pass class D(B,C): pass The precedence order for class D inheritance is: B, A, C. Tip
You always have to use fully qualified names when calling a superclass's method (if it has been overridden) because if the class has multiple base classes containing the same symbol, the first one found is used. >>> class A: …def __init__(self, name): … self.name = name …def printname(self): … print 'The name %s belongs to class A!'% self.name … >>> class B(A): …__baseclass=A …def __init__(self, name): … self.__ baseclass.__init__(self,name) …def printname(self): … print 'The name %s belongs to class B!'% self.name … self.__ baseclass.printname(self) … >>> class C(B): …__baseclass=B …def __init__(self, name): … self.__ baseclass.__init__(self,name) …def printname(self): … print 'The name %s belongs to class C!'% self.name … self.__ baseclass.printname(self) … >>> A("monkey").printname() The name monkey belongs to class A! >>> B("parrot").printname() The name parrot belongs to class B! The name parrot belongs to class A! >>> C("ant").printname() The name ant belongs to class C! The name ant belongs to class B! The name ant belongs to class A!
|
Index terms contained in this sectionbase classclasses base creating subclasses 2nd 3rd inheritance 2nd multiple inheritance object-oriented programming (OOP) inheritance 2nd programming object-oriented (OOP) inheritance 2nd subclass superclassing superclassÓ Ň writing subclasses 2nd 3rd |
© 2002, O'Reilly & Associates, Inc. |