Custom Search

Wednesday, December 8, 2010

Points to remember while Assigning and accessing class variables using 'self.' or object and class name in Python

Points to remember while Assigning and accessing class variables using 'self.' or object and class name in Python


class E:
"""
Points to remember while Assigning and accessing class variables using 'self.' or
object and class name.
"""
class_var_list = []
class_var_num = 100
class_var_str = 'SA-1'
class_var_dict = {}

def __init__(self):
self.name = 'saju'

def create_new_instance_variable_new_var_1(self, value):
#We can create an instance variable by assigne a value to a variable using 'self.'.
self.new_var_1 = value

def print_all_instance_vars(self):
return vars(self)

def get_value_from_new_var_1(self, new_var_1):
return self.new_var_1

def assigne_value_to_class_var_num_using_self(self, value):
#this create an instance variable, because assignment using 'self.'.
self.class_var_num = value

def assigne_value_to_class_var_num_using_class_name(self, value):
#equal to E.class_var_num = value
self.__class__.class_var_num = value

def assigne_value_to_class_var_str_using_self(self, value):
#this create an instance variable, because assignment using 'self.'.
self.class_var_str = value

def assigne_value_to_class_var_str_using_class_name(self, value):
self.__class__.class_var_str = value

def assigne_value_to_class_var_list_using_self(self, value):
#Here appending value to 'class_var_list' using 'self.'
#So it does not create new instance variable 'self.class_var_list'.
#Here 'self.class_var_list' point to class variable 'E.class_var_list', not to
'self.class_var_list'.
#Here not assigning new list to 'self.class_var_list'.
self.class_var_list.append(value)

def assigne_value_to_class_var_list_using_class_name(self, value):
#appending value to class variable 'E.class_var_list'
self.__class__.class_var_list.append(value)

def assigne_value_to_class_var_dict_using_self(self, key, value):
#Here 'self.class_var_dict' point to class variable 'E.class_var_dict', not to
'self.class_var_dict'.
#Here not assigning new dict to 'self.class_var_dict'.
#So it does not create new instance variable 'self.class_var_dict'.
self.class_var_dict[key] = value

def assigne_value_to_class_var_dict_using_class_name(self, key, value):
self.__class__.class_var_dict[key] = value

def get_value_from_class_var_num_using_self(self):
return self.class_var_num

def get_value_from_class_var_num_using_class_name(self):
return self.__class__.class_var_num

def get_value_from_class_var_str_using_self(self):
return self.class_var_str

def get_value_from_class_var_str_using_class_name(self):
return self.__class__.class_var_str

def get_value_from_class_var_list_using_self(self):
return self.class_var_list

def get_value_from_class_var_list_using_class_name(self):
return self.__class__.class_var_list

def get_value_from_class_var_dict_using_self(self):
return self.class_var_dict

def get_value_from_class_var_dict_using_class_name(self):
return self.__class__.class_var_dict

def reset_class_var_num_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_num = 800

def reset_class_var_str_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_str = 'SA-2'

def reset_class_var_list_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_list = []

def reset_class_var_dict_using_self(self):
#this create an instance variable, because assignment using 'self.'.
self.class_var_dict = {}

def reset_class_var_num_using_class_name(self):
self.__class__.class_var_num = 800

def reset_class_var_str_using_class_name(self):
self.__class__.class_var_str = 'SA-2'

def reset_class_var_list_using_class_name(self):
self.__class__.class_var_list = []

def reset_class_var_dict_using_class_name(self):
#equal to E.class_var_dict = {}
self.__class__.class_var_dict = {}

def get_all_local_vars(self):
return locals()

def get_all_global_vars():
return globals()


e1 = E()
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----e1.create_new_instance_variable_new_var_1(25)----",
e1.create_new_instance_variable_new_var_1(25)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_num_using_self(100)----",
e1.assigne_value_to_class_var_num_using_self(100)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_str_using_self(200)----",
e1.assigne_value_to_class_var_str_using_self(200)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_list_using_self(300)----",
e1.assigne_value_to_class_var_list_using_self(300)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e1.assigne_value_to_class_var_dict_using_self(400)----",
e1.assigne_value_to_class_var_dict_using_self('mark', 400)
print "----e1.print_all_instance_vars()----", e1.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict

print "\n\n--------------------------------------------------------------------"

e2 = E()
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----e2.create_new_instance_variable_new_var_1(25)----",
e2.create_new_instance_variable_new_var_1(25)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_num_using_class_name(100)----",
e2.assigne_value_to_class_var_num_using_class_name(100)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_str_using_class_name(200)----",
e2.assigne_value_to_class_var_str_using_class_name(200)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_list_using_class_name(300)----",
e2.assigne_value_to_class_var_list_using_class_name(300)
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e2.assigne_value_to_class_var_dict_using_class_name(400)----",
e2.assigne_value_to_class_var_dict_using_class_name('place', 'Banagalore')
print "----e2.print_all_instance_vars()----", e2.print_all_instance_vars
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict

print "\n\n--------------------------------------------------------------------"

e3 = E()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----e3.create_new_instance_variable_new_var_1(25)----",
e3.create_new_instance_variable_new_var_1(25)
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e3.reset_class_var_num_using_self()----", e3.reset_class_var_num_using_self()
print "----e3.reset_class_var_str_using_self()----", e3.reset_class_var_str_using_self()
print "----e3.reset_class_var_list_using_self()----", e3.reset_class_var_list_using_self()
print "----e3.reset_class_var_dict_using_self()----", e3.reset_class_var_dict_using_self()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict
print "----e3.reset_class_var_num_using_class_name()----",
e3.reset_class_var_num_using_class_name()
print "----e3.reset_class_var_str_using_class_name()----",
e3.reset_class_var_str_using_class_name()
print "----e3.reset_class_var_list_using_class_name()----",
e3.reset_class_var_list_using_class_name()
print "----e3.reset_class_var_dict_using_class_name()----",
e3.reset_class_var_dict_using_class_name()
print "----e3.print_all_instance_vars()----", e3.print_all_instance_vars()
print "----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name----", E.class_var_num, E.class_var_str ,
E.class_var_list, E.class_var_dict


OUTPUT
=======

----e1.print_all_instance_vars()---- {'name': 'saju'}
----e1.create_new_instance_variable_new_var_1(25)---- None
----e1.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_num_using_self(100)---- None
----e1.print_all_instance_vars()---- {'name': 'saju', 'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_str_using_self(200)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [] {}
----e1.assigne_value_to_class_var_list_using_self(300)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [300] {}
----e1.assigne_value_to_class_var_dict_using_self(400)---- None
----e1.print_all_instance_vars()---- {'class_var_str': 200, 'name': 'saju',
'class_var_num': 100, 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list,
E.class_var_dict using class name---- 100 SA-1 [300] {'mark': 400}


--------------------------------------------------------------------
----e2.print_all_instance_vars()---- {'name': 'saju'}
----e2.create_new_instance_variable_new_var_1(25)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 SA-1 [300] {'mark': 400}
----e2.assigne_value_to_class_var_num_using_class_name(100)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 SA-1 [300] {'mark': 400}
----e2.assigne_value_to_class_var_str_using_class_name(200)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300] {'mark': 400}
----e2.assigne_value_to_class_var_list_using_class_name(300)---- None
----e2.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'mark': 400}
----e2.assigne_value_to_class_var_dict_using_class_name(400)---- None
----e2.print_all_instance_vars()---- >
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}


--------------------------------------------------------------------
----e3.print_all_instance_vars()---- {'name': 'saju'}
----e3.create_new_instance_variable_new_var_1(25)---- None
----e3.print_all_instance_vars()---- {'name': 'saju', 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}
----e3.reset_class_var_num_using_self()---- None
----e3.reset_class_var_str_using_self()---- None
----e3.reset_class_var_list_using_self()---- None
----e3.reset_class_var_dict_using_self()---- None
----e3.print_all_instance_vars()---- {'class_var_str': 'SA-2', 'name': 'saju',
'class_var_num': 800, 'class_var_dict': {}, 'class_var_list': [], 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict
using class name---- 100 200 [300, 300] {'place': 'Banagalore', 'mark': 400}
----e3.reset_class_var_num_using_class_name()---- None
----e3.reset_class_var_str_using_class_name()---- None
----e3.reset_class_var_list_using_class_name()---- None
----e3.reset_class_var_dict_using_class_name()---- None
----e3.print_all_instance_vars()---- {'class_var_str': 'SA-2', 'name': 'saju',
'class_var_num': 800, 'class_var_dict': {}, 'class_var_list': [], 'new_var_1': 25}
----print E.class_var_num, E.class_var_str , E.class_var_list, E.class_var_dict using
class name---- 800 SA-2 [] {}

No comments:

Post a Comment