Untitled Document
OOP PART - 2 ( Class Varibales )
1. Creating and accessing class Variable Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Creating and accessing class Variable
       
# Class Varibales are the variables which are shared amongst all the instances of the class 
# IMP - You can access the class variables using instance of the class or By class name 
# here we will create raise_amount as a class variable which we will use in apply_raise method()

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):
		# Accesing class variable using class name
		self.pay = int(self.pay * Employee.raise_amount)
		# OR Access class varibale using instance 
		#self.pay = int(self.pay * self.raise_amount)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1.pay)
print(emp_1.fullname())
emp_1.apply_raise()
print(emp_1.pay)
       
2. Program - More Tips on accessing class variables Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - More Tips on accessing class variables 

# IMP - whenever we access any variable using object instance first it checks whether that variable is
# available in its own instance ; if yes then it will use that else it will go to check the class level 
# Lets understand this by using below code 

# In below example we don't have any raise_amount varibale available at instance level 
# You can confirm this by using highlighted code **

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):
		# Accesing class variable using class name
		self.pay = int(self.pay * Employee.raise_amount)
		# OR Access class varibale using instance 
		#self.pay = int(self.pay * self.raise_amount)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1.__dict__) #**
#In above print statement you will not get the raise_amount variable 
print(Employee.__dict__)
       
3. Program - Few more tips on class variables Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Few more tips on class variables  

# Just run the below example and try to analyse the result 

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):
		# Accesing class variable using class name
		self.pay = int(self.pay * Employee.raise_amount)
		# OR Access class varibale using instance 
		#self.pay = int(self.pay * self.raise_amount)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

emp_1.raise_amount = 1.05

print(Employee.raise_amount)
print(emp_1.raise_amount) # The raise_amount value is changed only for emp_1 instance
print(emp_2.raise_amount)
       
4. Program - A more Practical example on class varibale Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - A more Practical example on class varibale 
# Here we have created fullname() method inside a class which will return fullname   
       
# In below example we will count the number of instacne or objects are created for the class. 

class Employee:

	raise_amount = 1.04
	instance_count = 0

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last + '@company.com'

		Employee.instance_count +=1

	def fullname(self):
		return '{} {}'.format(self.first,self.last)

	def apply_raise(self):
		# Accesing class variable using class name
		self.pay = int(self.pay * Employee.raise_amount)
		# OR Access class varibale using instance 
		#self.pay = int(self.pay * self.raise_amount)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(Employee.instance_count)
       
Untitled Document