Custom Search

Wednesday, December 8, 2010

Python Child and Super class Construction Tricks-3

Python Child and Super class Construction Tricks-3

Here method 'function_in_base_and_child_class_1_and_2' is not defined in Super class
'Base_class'.
This will cause AttributeError when tring to call method
'function_in_base_and_child_class_1_and_2' using object of class 'Base_class'.

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

class Base_Class:

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

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"

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"

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"

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


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

<<-------------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>

<<-------------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>

<<-------------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>
Traceback (most recent call last):
File "base_class.py", line 56, in
c3.test_function_in_base_class()
File "base_class.py", line 14, in test_function_in_base_class
self.function_in_base_and_child_class_1_and_2()
AttributeError: Base_Class instance has no attribute 'function_in_base_and_child_class_1_and_2'

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

No comments:

Post a Comment