-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinit , class method.py
More file actions
51 lines (43 loc) · 1.43 KB
/
Copy pathinit , class method.py
File metadata and controls
51 lines (43 loc) · 1.43 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Employee :
no =4
pass
def __init__(self,name,age,pay):
self.name=name
self.age=age
self.pay=pay
def toprint(self):
return ("name is" ,self.name ,"age is", self.age ,"pay is", self.pay)
@classmethod # do use @
def change_no(cls, new_no):
cls.no = new_no
harry= Employee("harry",21,321)
larry= Employee("larry",22,432)
parry= Employee("parry",23,543)
print(harry.toprint())
#phle instance variable me dhundega agr nhi mila to class variable p jaega...."no" is class variable.."name/pay/age are instance variable....harry/larry/parry are objects
#we can print "no" by calling harry/larry/parry also
print(harry.no)
print(Employee.no)
harry.no=6
print("______")
print(harry.no)
print(larry.no)
print(parry.no)
print(Employee.no)
#but if we try to chnge the value of "no" by calling objects then it will not get chnged it will create a new attribute
#we can chnge the value of "no" using class only
Employee.no=8
print("______")
print(harry.no)
print(larry.no)
print(parry.no)
print(Employee.no)
print("______")
#now we can change value of class variable by any instance using class method...(harry vala chnge nhi hoga bcz humne esko phele hi nya variable bna dia...
# ar phele hmesha instance variable m dhundta h..bd m class vale m)
harry.change_no(45)
print("_____")
print(harry.no)
print(larry.no)
print(parry.no)
print(Employee.no)