Can you please tell me what is a difference between calling Class_name.class_variable and self.class_variable inside method. Here is the example:
class Employee:
raise_amount = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
def apply_raise(self):
self.pay = int(self.pay * Employee.raise_amount)
So I used an Employee.raise_amount, but i can also write this method like that:
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)
I tested that with:
emp_1 = Employee('James', 'Amb', 10000)
emp_2 = Employee('Test', 'User', 20000)
print("Original value")
print("emp_1.raise_amount", emp_1.raise_amount)
print("emp_2.raise_amount", emp_2.raise_amount)
emp_1.raise_amount = 1.1
print("emp_1.raise_amount = 1.1")
print("emp_1.raise_amount", emp_1.raise_amount)
print("emp_2.raise_amount", emp_2.raise_amount)
Employee.raise_amount = 1.2
print("Employee.raise_amount = 1.2")
print("emp_1.raise_amount", emp_1.raise_amount)
print("emp_2.raise_amount", emp_2.raise_amount)
I run the program using Employee.raise_amount and then self.raise_amount. In both situation OUTPUT is the same:
Original value
emp_1.raise_amount 1.04
emp_2.raise_amount 1.04
emp_1.raise_amount = 1.1
emp_1.raise_amount 1.1
emp_2.raise_amount 1.04
Employee.raise_amount = 1.2
emp_1.raise_amount 1.1
emp_2.raise_amount 1.2
So what is a difference and when should I use Class_name.class_variable and self.class_variable