Custom Search

Wednesday, December 8, 2010

Python Child and Super class Construction Tricks-1

Python Child and Super class Construction Tricks-1

call methods in child and super class using child class 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 function_in_base_class_only(self):
print "-----Base_Class------function_in_base_class_only-----self----->>>, ", self

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

def function_in_child_class_1_only(self):
print "-----Child_Class_1------function_in_child_class_1_only----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

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

print "......c1 = Child_Class_1()......."
c1 = Child_Class_1()
print "......c1.function_in_base_and_child_class_1_and_2()......."
c1.function_in_base_and_child_class_1_and_2()
print "......c1.function_in_child_class_1_only()......."
c1.function_in_child_class_1_only()
print "......c1.function_in_base_class_only()......."
c1.function_in_base_class_only()

print "\n<<---------------------------->>\n"

print "......c2 = Child_Class_2()......"
c2 = Child_Class_2()
print "......c2.function_in_base_and_child_class_1_and_2()......."
c2.function_in_base_and_child_class_1_and_2()
print "......c2.function_in_child_class_2_only()......"
c2.function_in_child_class_2_only()
print "......c2.function_in_child_class_1_only()......"
c2.function_in_child_class_1_only()
print "......c2.function_in_base_class_only()......"
c2.function_in_base_class_only()

print "\n<<---------------------------->>\n",

print "-----------vars(Child_Class_2)----------\n", vars(Child_Class_2)

OUTPUT
=======
# python base_class.py

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


* Note: Here same instance or object or '0x7f52a3dcfa70' of class 'Child_Class_1',
is passing as 'self', to all methods calling using that object 'c1'.
* Note: Here same 'self'('0x7f52a3dcfa70'), is passing to all methods,
calling using object 'c1'.
* Note: Here c1 is just a name point to object location '0x7f52a3dcfa70'.

<<---------------------------->>

......c2 = Child_Class_2()......
-----Child_Class_2------__init__----->>>, <__main__.child_class_2>
......c2.function_in_base_and_child_class_1_and_2().......
-----Child_Class_2------function_in_base_and_child_class_1_and_2----self---->>>,
<__main__.child_class_2>
......c2.function_in_child_class_2_only()......
-----Child_Class_2------function_in_child_class_2_only-----self----->>>,
<__main__.child_class_2>
......c2.function_in_child_class_1_only()......
-----Child_Class_1------function_in_child_class_1_only----self----->>>,
<__main__.child_class_2>
......c2.function_in_base_class_only()......
-----Base_Class------function_in_base_class_only-----self----->>>,
<__main__.child_class_2>


* Note: Here same instance or object or '0x7f52a3dcfab8' of class 'Child_Class_2',
is passing as 'self', to all methods calling using that object 'c2'.
* Note: Here same 'self' or '0x7f52a3dcfab8', is passing to all methods,
calling using object 'c2'.
* Note: Here c2 is just a name point to object location '0x7f52a3dcfab8'.

<<---------------------------->>
-----------vars(Child_Class_2)----------

{
'__module__': '__main__',
'__doc__': None,
'function_in_child_class_2_only': ,
'__init__': ,
'function_in_base_and_child_class_1_and_2':
}

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

No comments:

Post a Comment