Dec-10-2019, 03:46 PM
Hello everyone, I am learning OOP in python, specifically class variables. It is clear to me what they are, but I do not get why they should be accessed through the instance. Referring to the following tutorial in the method apply_raise the variable can be accessed through the instance (line 15 --> self.pay = int( self.pay * self.raise_amount )). I tried to access the variable through the class (line 16 --> self.pay = int( self.pay * Employee.raise_amount ), and it works in the same way, at least it looks so to me. Is there any difference?
class Employee: raise_amount = 1.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 ) #self.pay = int( self.pay * Employee.raise_amount ) return self.pay