Sun classing - Python

A 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:
lFrom scratch
>>>class A:
… pass

lBy using single inheritance
>>>class B(A):
… pass

lBy using multiple inheritance
>>>class D(B,C):
… pass

For a conceptual standpoint, take a look at the following example

Base class = writing tools
subclass = pen
subclass = chalk

Both subclasses pen and chalk inherit characteristics of the base class writing tools. The subsequent class defines a complex class called Employee.

class Employee:
def __init__(self,name,salary=0): = name
self.salary = salary = []
defraisesalary(self, percent):
self.salary = self.salary + (self.salary * percent)
def work (self):
print, "writes computer code."
returnlen( == 0 # returns a boolean result
defaddmember(self, x):
defremovemember(self, x):
iflen( > 0:
x =[-1][-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, "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:
class B(A):
class C:
class D(B,C):

The precedence order for class D inheritance is: B, A, C.

>>>class A:
…def __init__(self, name):
… = name
… print 'The name %s belongs to class A!'%

>>>class B(A):
…def __init__(self, name):
… self.__ baseclass.__init__(self,name)
… print 'The name %s belongs to class B!'%
… self.__ baseclass.printname(self)

>>>class C(B):
…def __init__(self, name):
… self.__ baseclass.__init__(self,name)
… print 'The name %s belongs to class C!'%
… self.__ baseclass.printname(self)

The name monkey belongs to class A!
The name parrot belongs to class B!
The name parrot belongs to class A!
The name ant belongs to class C!
The name ant belongs to class B!
The name ant belongs to class A!

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Python Topics