-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassandstaticmethod.py
More file actions
34 lines (22 loc) · 1008 Bytes
/
classandstaticmethod.py
File metadata and controls
34 lines (22 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Student:
school = "ABC School" #class variable
def __init__(self, m1, m2, m3): #m1, m2, m3 are instance variables
self.m1 = m1
self.m2 = m2
self.m3 = m3
def avg(self): # normal method
return (self.m1 + self.m2 + self.m3)/3
#class method is used when we are using class variables
@classmethod #decorators
def getSchoolName(cls): #cls is used to access class variables
return cls.school
#static method is used when neither class variable nor instance variable is required
@staticmethod
def info(): #This method takes neither instance variables nor class variable, hence it is called static method
print("This is student class ... in ABC School")
s1 = Student(34, 56, 76)
s2 = Student(56,23,89)
print("Average of student 1 is ", s1.avg())
print("Average of student 2 is ", s2.avg())
print(Student.getSchoolName())
Student.info()