-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47_instance_class_static.py
More file actions
44 lines (30 loc) · 945 Bytes
/
Copy path47_instance_class_static.py
File metadata and controls
44 lines (30 loc) · 945 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
35
36
37
38
39
40
41
42
43
44
class Employee:
company="HP"
def __init__(self,name,salary):
self.name=name
self.salary=salary
# this is Instance method (bydefault)
def print_info(self):
print(f"name of employee is {self.name} and salary is {self.salary}")
# this is static method
@staticmethod # if this is not used then if we call the function then it will give an error
def sum(a,b):
return a+b
#class method
@classmethod
def change_company(cls,new_company):
cls.company=new_company
@classmethod
def print_company(cls):
print(cls.company)
e1=Employee("Shubham",500000)
e2=Employee("Dhar",120000)
# print(Employee.company)
# print(e1.name," ",e1.salary)
# print(Employee.name)# error
e1.print_info()
e2.print_info()
print(e1.sum(3,8))
print(Employee.company)
e2.change_company("Dell")
print(Employee.company)