2021-02-01 python►b-OOP Python-b01-Classes and Instances This article shows the examples of Classes and Instances in python OOP: Object-Oriented Programming 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263# basicclass Employee: def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.email = first + '.' + last + '@company.com' def fullname(self): return '{} {}'.format(self.first, self.last)emp_1 = Employee('Bob', 'Gobdo', 20)print(emp_1.email)print(emp_1.fullname())# the same thing of fullname()print(Employee.fullname(emp_1))# class variablesclass Employee: raise_amount = 2.04 def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.email = first + '.' + last + '@company.com' def fullname(self): return '{} {}'.format(self.first, self.last) def apply_raise(self): self.pay = int(self.pay * self.raise_amount) # raise_amount is class vriable, we can remplace by Employee.raise_amountemp_1 = Employee('Bob', 'Gobdo', 20)print(emp_1.pay) # 20emp_1.apply_raise()print(emp_1.pay) # 40print(Employee.__dict__) # there is raise_amount insideprint(emp_1.__dict__) # there is no raise_amount inside# change raise_amount by instanceemp_1 = Employee('Bob', 'Gobdo', 20)emp_2 = Employee('John', 'Youhs', 30)emp_1.raise_amount = 3print(Employee.raise_amount) # 2.04print(emp_1.raise_amount) # 3print(emp_2.raise_amount) # 2.04# change raise_amount by classemp_1 = Employee('Bob', 'Gobdo', 20)emp_2 = Employee('John', 'Youhs', 30)Employee.raise_amount = 3print(Employee.raise_amount) # 3print(emp_1.raise_amount) # 3print(emp_2.raise_amount) # 3 Newer Python-b02-classmethod-staticmethod Older Python-a09-Modules