Class variables are the ones, which are sharable among all the instances of the class. The class variable must be the same for all the instances. To understand with example, let's assume that leapx_org
gives 5 percent increment based upon pay_amt
. Let's use another method to calculate the increment. Refer to the classinstance1.py
program:
class Leapx_org(): def __init__(self,first,last,pay): self.f_name = first self.l_name = last self.pay_amt = pay self.full_name = first+" "+last def make_email(self): return self.f_name+ "."+self.l_name+"@xyz.com" def incrementpay(self): self.pay_amt = int(self.pay_amt*1.20) return self.pay_amt L_obj1 = Leapx_org('mohit', 'RAJ', 60000) L_obj2 = Leapx_org('Ravender', 'Dahiya',70000) print L_obj1.pay_amt print L_obj1.incrementpay()
There are a couple of things that are new in the preceding program. We added incrementpay()
which returns the raised pay_amt amount
. The last line print L_obj1.incrementpay()
states...