PYTHON NOTES JEGAN K K
COLLEGE AND STUDENTS LIST USING DICTIONARY AND CLASS
clgdict = dict()
class College (clg):
NameList = []
def Student (stu):
NameList.append(stu)
clgdict[clg] = NameList
return Student
clg1 = College('ABC College')
clg2 = College('XYZ College')
clg1('Jegan')
clg1('A')
clg2('Hari')
clg2('B')
In the above example, outer class college receives the college name and is created as a
reference. The Inner class parameter is passed with outer class reference. So the names of
student called with respective college instance gets appended to the list (i.e. NameList), thus
maintaining the student record for each college respectively.
NOTE: Even if class college is deleted, the reference works.
CLASS:
Class is a blueprint or a prototype that is set initially to create instance with it and can be used
later. The previous example is a class with a sub-class defined within.
Syntax:
class Class_name:
def __init__(self):
print ('A Reference Instantiated')
In the above example, a class is instantiated or created along with a method which is called the
default constructor, 2 underscores followed by 'init' followed by 2 underscores again. It runs by
default when a reference is initiated. The syntax to initiate is given below,
Syntax: cls_ref = class_name()
We can also send default parameter while initiating a reference inside the '()' which is followed
after class_name. It is sent as a parameter to the default constructor __init__(). We should
define the default constructor in such a way that it receives a parameter and for initiating it we
use the self keyword (self.parameter = parameter).
COLLEGE AND STUDENTS LIST USING DICTIONARY AND CLASS
clgdict = dict()
class College (clg):
NameList = []
def Student (stu):
NameList.append(stu)
clgdict[clg] = NameList
return Student
clg1 = College('ABC College')
clg2 = College('XYZ College')
clg1('Jegan')
clg1('A')
clg2('Hari')
clg2('B')
In the above example, outer class college receives the college name and is created as a
reference. The Inner class parameter is passed with outer class reference. So the names of
student called with respective college instance gets appended to the list (i.e. NameList), thus
maintaining the student record for each college respectively.
NOTE: Even if class college is deleted, the reference works.
CLASS:
Class is a blueprint or a prototype that is set initially to create instance with it and can be used
later. The previous example is a class with a sub-class defined within.
Syntax:
class Class_name:
def __init__(self):
print ('A Reference Instantiated')
In the above example, a class is instantiated or created along with a method which is called the
default constructor, 2 underscores followed by 'init' followed by 2 underscores again. It runs by
default when a reference is initiated. The syntax to initiate is given below,
Syntax: cls_ref = class_name()
We can also send default parameter while initiating a reference inside the '()' which is followed
after class_name. It is sent as a parameter to the default constructor __init__(). We should
define the default constructor in such a way that it receives a parameter and for initiating it we
use the self keyword (self.parameter = parameter).