Custom Search

Wednesday, December 8, 2010

Python Child and Super class Construction Tricks-2

Python Child and Super class Construction Tricks-2

call method in Super class, that method will call method of calling object.

--------------------------------------base_class.py

class Base_Class:

def __init__(self):
print "-----Base_Class------__init__----->>>, ", self
self.name = 'saju'

def function_in_base_and_child_class_1_and_2(self):
print "-----Base_Class------function_in_base_and_child_class_1_and_2----self----->>>,
", self
#raise Exception "need to implement in child class"

def test_function_in_base_class(self): #<----------- Note this method.
print "-----Base_Class------test_function_in_base_class-----self----->>>, ", self
self.function_in_base_and_child_class_1_and_2()


class Child_Class_1(Base_Class):

def __init__(self):
print "-----Child_Class_1------__init__----->>>, ", self
self.age=25

def function_in_base_and_child_class_1_and_2(self):
print "-----Child_Class_1------function_in_base_and_child_class_1_and_2----self---->>>,
", self


class Child_Class_2(Child_Class_1):

def __init__(self):
print "-----Child_Class_2------__init__----->>>, ", self
self.salary=5000

def function_in_base_and_child_class_1_and_2(self):
print "-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>,
", self


print "\n-------------CASE-1--------------->>\n"
#Calling method 'test_function_in_base_class' in Super class using child class object.
#Here Super class method 'test_function_in_base_class' will call method '
function_in_base_and_child_class_1_and_2' fo calling object.

print "......c2 = Child_Class_2()......"
c2 = Child_Class_2()
print "......c2.test_function_in_base_class()......."
c2.test_function_in_base_class()

print "\n-------------CASE-2--------------->>\n"
#Calling method in Super class using child class object.
#Here Super class method 'test_function_in_base_class' will call method
'function_in_base_and_child_class_1_and_2' fo calling object.

print "......c1 = Child_Class_1()......"
c1 = Child_Class_1()
print "......c1.test_function_in_base_class()......."
c1.test_function_in_base_class()


print "\n-------------CASE-3--------------->>\n"
#Calling method in Super class using Super class object.
#Here Super class method 'test_function_in_base_class' will call method
'function_in_base_and_child_class_1_and_2' fo calling object.

print "......c3 = Base_Class()......"
c3 = Base_Class()
print "......c3.test_function_in_base_class()......."
c3.test_function_in_base_class()


OUTPUT
=======
-------------CASE-1--------------->>

......c2 = Child_Class_2()......
-----Child_Class_2------__init__----->>>, <__main__.child_class_2>
......c2.test_function_in_base_class().......
-----Base_Class------test_function_in_base_class-----self----->>>, <__main__.child_class_2>
-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>, <
__main__.child_class_2>

* Calling method 'test_function_in_base_class' in Super class using child class object.
* Here Super class method 'test_function_in_base_class' will call method
'function_in_base_and_child_class_1_and_2' fo calling object.
* Note: Here method 'test_function_in_base_class' of class 'Base_Class'
will call method 'function_in_base_and_child_class_1_and_2' of class 'Child_Class_2',
because here calling the method 'test_function_in_base_class' of class
'Base_Class' using the object of the child class 'Child_Class_2'.

-------------CASE-2--------------->>

......c1 = Child_Class_1()......
-----Child_Class_1------__init__----->>>, <__main__.child_class_1>
......c1.test_function_in_base_class().......
-----Base_Class------test_function_in_base_class-----self----->>>, <__main__.child_class_1>
-----Child_Class_1------function_in_base_and_child_class_1_and_2----self---->>>,
<__main__.child_class_1>

* Calling method 'test_function_in_base_class' in Super class using child class object.
* Here Super class method 'test_function_in_base_class' will call method
'function_in_base_and_child_class_1_and_2' fo calling object.
* Note: Here method 'test_function_in_base_class' of class 'Base_Class' w
ill call method 'function_in_base_and_child_class_1_and_2' of class 'Child_Class_1',
because here calling the method 'test_function_in_base_class' of class
'Base_Class' using the object of the child class 'Child_Class_1'.

-------------CASE-3--------------->>

......c3 = Base_Class()......
-----Base_Class------__init__----->>>, <__main__.base_class>
......c3.test_function_in_base_class().......
-----Base_Class------test_function_in_base_class-----self----->>>, <__main__.base_class>
-----Base_Class------function_in_base_and_child_class_1_and_2----self----->>>,
<__main__.base_class>

* Calling method 'test_function_in_base_class' in Super class using Super class object.
* Here Super class method 'test_function_in_base_class' will call method
'function_in_base_and_child_class_1_and_2' fo calling object.
* Note: Here method 'test_function_in_base_class' of class 'Base_Class'
will call method 'function_in_base_and_child_class_1_and_2' of class 'Base_Class',
because here calling the method 'test_function_in_base_class' of class
'Base_Class' using the object of the class 'Base_Class'.

=========================================

No comments:

Post a Comment