Class variables

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 that the L_obj1 instance calls the incrementpay() method. Let's run the program:

Output of classinstance1.py

The preceding program runs successfully but most of its content is logically wrong. In the incrementpay() method, we used the multiplication number 1.20, which would be same for all the instances. So we can make the multiplication number 1.20 as the class variable.

Let's write the new code classinstance2.py with amendments:

class Leapx_org():
mul_num = 1.20
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*self.mul_num)
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()

In the preceding program, we made a mul_num class variable that contains the value 1.20. In the incrementpay() method, we access the mul_num class variable with the help of self means instance. You can use either self or class name with the mul_num. If you use the class name with mul_num, then it would be Leapx_org. mul_num. If you don't use any of them, then the interpreter throws an error. You might be confused, if it is a class variable, then how can we access the class variable with the instance. Let's add some lines to understand it better. Add the following lines at the end of the code:

print L_obj1.mul_num
print L_obj2.mul_num
print Leapx_org.mul_num

Run the program and see the output:

Output of program classinstance2 with added lines

As you can see, we are accessing the mul_num class variable with the help of instances L_obj1, L_obj2, and the Leapx_org class. All are showing the same value 1.2. So what happens when we try to access an attribute by an instance? The instance first checks whether the instance contains the attribute. If the instance does not contain the attribute, then it checks the class or its parent class contains that attribute. So the instances L_obj1 and L_obj2 access the mul_num from the class. For more clarification, you can view the attribute of the class and instances.

The following is the full code of classinstance3.py:

class Leapx_org():
mul_num = 1.20
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*self.mul_num)
return self.pay_amt

L_obj1 = Leapx_org('mohit', 'RAJ', 60000)
L_obj2 = Leapx_org('Ravender', 'Dahiya',70000)
print "instance space ",L_obj1.__dict__
print "class space ",Leapx_org.__dict__

Except for the last two lines, the rest of the code is similar to the previous one. The L_obj1.__dict__ syntax prints all the attributes of the L_obj1 instance and Leapx_org.__dict__ prints all the attributes of the Leapx_org class. Run the program to see the output:

Output of code classinstance3.py

You can see that the instance name space does not contain the mul_num class variable, but the class name space contains mul_num. Let's add mul_num to the name space of the L_obj1 instance.  To avoid confusion, we will write classinstance4.py:

class Leapx_org():
mul_num = 1.20
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*self.mul_num)
return self.pay_amt

L_obj1 = Leapx_org('mohit', 'RAJ', 60000)
L_obj2 = Leapx_org('Ravender', 'Dahiya',70000)
L_obj1.mul_num = 1.3

print "instance space L_obj1 n",L_obj1.__dict__
print "ninstance space L_obj2 n",L_obj2.__dict__
print "nclass space n",Leapx_org.__dict__

print L_obj1.mul_num
print L_obj2.mul_num
print Leapx_org.mul_num

In the preceding code, line L_obj1.mul_num = 1.3 adds the mul_num variable in the name space of the L_obj1 instance. The last three lines of code print the name of space of the instance L_obj1, L_obj2 and the Leapx_org class. Let's run the code:

Output of code classinstance4.py

The preceding output shows that the L_obj1 instance finds the mul_num first with its own namespace before searching in the class namespace. That's why L_obj1 shows 1.3. We did not set mul_num, for instance, L_obj2, so L_obj2 is still getting the value from the class namespace. In the incrementpay() method, we use self.mul_num instead of Leapx_org.mul_num because self.mul_num gives the ability to change mul_num value for single instance, if we want to. Let's create one more class variable to count the number of employees.

Let us see the next code classinstance5.py:

class Leapx_org():
mul_num = 1.20
count= 0
def __init__(self,first,last,pay):
self.f_name = first
self.l_name = last
self.pay_amt = pay
self.full_name = first+" "+last
Leapx_org.count = Leapx_org.count+1

def make_email(self):
return self.f_name+ "."+self.l_name+"@xyz.com"

def incrementpay(self):
self.pay_amt = int(self.pay_amt*self.mul_num)
return self.pay_amt

L_obj1 = Leapx_org('mohit', 'RAJ', 60000)
L_obj2 = Leapx_org('Ravender', 'Dahiya',70000)
L_obj3 = Leapx_org('Bhaskar', 'DAS',70000)
print "Number of Employees are : ", Leapx_org.count

Earlier we created new class variables count initializing with 0. The syntax Leapx_org.count = Leapx_org.count+1 increases the class variable by one. We have created three instances. Whenever we create a new instance, the count variable is incremented by one. Let's see the output:

Output of code classinstance5.py

Now you got the idea of the class variable. If you set self.count = self.count+1 instead of Leapx_org.count = Leapx_org.count+1, then you would get 0 employees.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.237