#Python Child and Super Class with class variables
class Base_Class:
int_class_var_in_base_cls = 100
list_class_var_in_base_cls = [10]
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_child_class_1_only(self):
print "-----Child_Class_1------function_in_child_class_1_only----self---->>>, ", self
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_child_class_2_only(self):
print "-----Child_Class_2------function_in_child_class_2_only----self---->>>, ", self
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=============== 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 "......c2.function_in_child_class_1_only()......."
c2.function_in_child_class_1_only()
print "......c2.function_in_child_class_2_only()......."
c2.function_in_child_class_2_only()
print "......c2.function_in_base_and_child_class_1_and_2()......."
c2.function_in_base_and_child_class_1_and_2()
print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls
print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)
print "\n=============== 2 ===============\n"
print "-----c2.int_class_var_in_base_cls = 2400---------"
c2.int_class_var_in_base_cls = 2400
print "-----c2.list_class_var_in_base_cls = [560]---------"
c2.list_class_var_in_base_cls = [560]
print "\n----Assigning value to a variable(class variable) using 'self.' or
object should create new instance or member variable of \
that name (class variable name) for that object -----\n"
print "\n----Appending values to a lis type class variable and adding new key value
pair to a dict type class variable should not \
create new instance or member variable----\n"
print "\n----After this we can not access class variables 'int_class_var_in_base_cls'
and 'list_class_var_in_base_cls' using \
'self.' or object, because now that object have the instance or member variables of
name 'int_class_var_in_base_cls' and 'list_class_var_in_base_cls'.\
If we trying to access a variable using 'self.' or object, it first search in object
scope for that variable, then in class scope, then in global scope, then in module
level.----\n"
print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls
print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)
print "\n=============== 3 ===============\n"
print "-----Base_Class.int_class_var_in_base_cls = 1000---------"
Base_Class.int_class_var_in_base_cls = 1000
print "-----Base_Class.list_class_var_in_base_cls = [600]---------"
Base_Class.list_class_var_in_base_cls = [600]
print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls
print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)
print "\n=============== 4 ===============\n"
print "-----Child_Class_1.int_class_var_in_base_cls = 2000---------"
Child_Class_1.int_class_var_in_base_cls = 2000
print "-----Child_Class_1.list_class_var_in_base_cls = [700]---------"
Child_Class_1.list_class_var_in_base_cls = [700]
print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls
print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)
print "\n=============== 5 ===============\n"
print "-----Child_Class_2.int_class_var_in_base_cls = 3000---------"
Child_Class_2.int_class_var_in_base_cls = 3000
print "-----Child_Class_2.list_class_var_in_base_cls = [800]---------"
Child_Class_2.list_class_var_in_base_cls = [800]
print "-----c2.int_class_var_in_base_cls---------", c2.int_class_var_in_base_cls
print "-----c2.list_class_var_in_base_cls---------", c2.list_class_var_in_base_cls
print "-----Base_Class.int_class_var_in_base_cls---------",
Base_Class.int_class_var_in_base_cls
print "-----Base_Class.list_class_var_in_base_cls---------",
Base_Class.list_class_var_in_base_cls
print "-----Child_Class_1.int_class_var_in_base_cls---------",
Child_Class_1.int_class_var_in_base_cls
print "-----Child_Class_1.list_class_var_in_base_cls---------",
Child_Class_1.list_class_var_in_base_cls
print "-----Child_Class_2.int_class_var_in_base_cls---------",
Child_Class_2.int_class_var_in_base_cls
print "-----Child_Class_2.list_class_var_in_base_cls---------",
Child_Class_2.list_class_var_in_base_cls
print "\nPrint instance variables, class variables and methods associated with this object"
print "----------dir(c2)---------------", dir(c2)
print "\nPrint instance variables with value associated with this object"
print "----------vars(c2)---------------", vars(c2)
print "\n=============== 6 ===============\n"
OUTPUT
=======
# python base_class.py
=============== 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>
......c2.function_in_child_class_1_only().......
-----Child_Class_1------function_in_child_class_1_only----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_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.int_class_var_in_base_cls--------- 100
-----c2.list_class_var_in_base_cls--------- [10]
-----Base_Class.int_class_var_in_base_cls--------- 100
-----Base_Class.list_class_var_in_base_cls--------- [10]
-----Child_Class_1.int_class_var_in_base_cls--------- 100
-----Child_Class_1.list_class_var_in_base_cls--------- [10]
-----Child_Class_2.int_class_var_in_base_cls--------- 100
-----Child_Class_2.list_class_var_in_base_cls--------- [10]
Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']
Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000}
=============== 2 ===============
-----c2.int_class_var_in_base_cls = 2400---------
-----c2.list_class_var_in_base_cls = [560]---------
----Assigning value to a variable(class variable) using 'self.' or object should
create new instance or member variable of that name (class variable name) for
that object -----
----Appending values to a lis type class variable and adding new key value pair to a
dict type class variable should not create new instance or member variable----
----After this we can not access class variables 'int_class_var_in_base_cls' and
'list_class_var_in_base_cls' using 'self.' or object, because now that object have
the instance or member variables of name 'int_class_var_in_base_cls' and
'list_class_var_in_base_cls'.If we trying to access a variable using 'self.'
or object, it first search in object scope for that variable, then in class scope,
then in global scope, then in module level.----
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 100
-----Base_Class.list_class_var_in_base_cls--------- [10]
-----Child_Class_1.int_class_var_in_base_cls--------- 100
-----Child_Class_1.list_class_var_in_base_cls--------- [10]
-----Child_Class_2.int_class_var_in_base_cls--------- 100
-----Child_Class_2.list_class_var_in_base_cls--------- [10]
Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']
Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}
=============== 3 ===============
-----Base_Class.int_class_var_in_base_cls = 1000---------
-----Base_Class.list_class_var_in_base_cls = [600]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 1000
-----Child_Class_1.list_class_var_in_base_cls--------- [600]
-----Child_Class_2.int_class_var_in_base_cls--------- 1000
-----Child_Class_2.list_class_var_in_base_cls--------- [600]
Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']
Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}
=============== 4 ===============
-----Child_Class_1.int_class_var_in_base_cls = 2000---------
-----Child_Class_1.list_class_var_in_base_cls = [700]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 2000
-----Child_Class_1.list_class_var_in_base_cls--------- [700]
-----Child_Class_2.int_class_var_in_base_cls--------- 2000
-----Child_Class_2.list_class_var_in_base_cls--------- [700]
Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']
Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}
=============== 5 ===============
-----Child_Class_2.int_class_var_in_base_cls = 3000---------
-----Child_Class_2.list_class_var_in_base_cls = [800]---------
-----c2.int_class_var_in_base_cls--------- 2400
-----c2.list_class_var_in_base_cls--------- [560]
-----Base_Class.int_class_var_in_base_cls--------- 1000
-----Base_Class.list_class_var_in_base_cls--------- [600]
-----Child_Class_1.int_class_var_in_base_cls--------- 2000
-----Child_Class_1.list_class_var_in_base_cls--------- [700]
-----Child_Class_2.int_class_var_in_base_cls--------- 3000
-----Child_Class_2.list_class_var_in_base_cls--------- [800]
Print instance variables, class variables and methods associated with this object
----------dir(c2)--------------- ['__doc__', '__init__', '__module__',
'function_in_base_and_child_class_1_and_2', 'function_in_child_class_1_only',
'function_in_child_class_2_only', 'int_class_var_in_base_cls',
'list_class_var_in_base_cls', 'salary', 'test_function_in_base_class']
Print instance variables with value associated with this object
----------vars(c2)--------------- {'salary': 5000, 'int_class_var_in_base_cls': 2400,
'list_class_var_in_base_cls': [560]}
=============== 6 ===============
Custom Search
Thursday, December 9, 2010
Python Child and Super Class with class variables
Python Child and Super Class with class variables
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment