Date:

Share:

How to Create Instance Variables and Class Variables in Python

Related Articles

In this article I will explain how to create instance and class variables in Python. Aside from class variables and instance variables, we can also define variables within a method. In this case, these variables are local variables and can only be used within this method.

Furthermore, we access the instance variables using the object name. Whereas class variables are accessed using the class name. Moreover, the value of the class variables remains the same for each object. Admittedly, instance variables are given different values ​​for different objects.

The following example shows the use of instance variables and class variables. As can be seen, the variables A and B They are class variables. Therefore, the values ​​of these two variables remain the same for all objects of the class. In contrast, variables x and y are instance variables. Thus, these variables are assigned different values ​​for each object of the class. We also pass the values ​​of instance variables as parameters to the constructor while creating the objects. Moreover, we can access the instance variables using the self-keyword. Admittedly, we access the class variables using the class name. The following example also shows how to access and use instance variables and class variables in an expression. The show () method defined in the class calculates and displays the sum of these four variables.

class MyClass:
	a=100
	b=200
	def __init__(self, x, y):
		self.x=x
		self.y=y
	def show(self):
		print("a = ", MyClass.a)
		print("b = ", MyClass.b)
		print("x = ", self.x)
		print("y = ", self.y)
		print("sum = ", MyClass.a+MyClass.b+self.x+self.y)


ob1=MyClass(670, 433)
ob1.show()

ob2=MyClass(56, 24)
ob2.show()

ob3=MyClass(6, 3)
ob3.show()

Productivity

Example of displaying instance and class variables in Python

Use of class variables and instance variables in the EmployeePay class

Another example is shown here. Basically, the EmployeePay class contains three class variables. These variables are hra, da and deductions. So, each object of this class uses the same values ​​for these variables. However, the class has two instance variables. These renew, and represent the name of the basic employee, and basic salary. The class also has a showNetPay () method. The method calculates the net salary of an employee. Moreover, it contains a term that includes the use of both types of variables. The next section shows the full code. Also, pay attention to the use of a self-keyword.

class EmployeePay:
	hra=0.23
	da=0.5
	deductions=5000
	def __init__(self, ename, basic):
		self.ename=ename
		self.basic=basic
	def showNetPay(self):
		net=EmployeePay.hra*self.basic+EmployeePay.da*self.basic-EmployeePay.deductions+self.basic
		print("Employee Name: ", self.ename)
		print("Basic Salary: ", self.basic)
		print("Net Salary: ", net)

print("First Object...")
ob1=EmployeePay("Pratham", 10000)
ob1.showNetPay()

print("Second Object...")
ob2=EmployeePay("Aditya", 14000)
ob2.showNetPay()

print("Third Object...")
ob3=EmployeePay("Ankur", 21000)
ob3.showNetPay()

Productivity

Using instance variables and class variables together in Python
Using instance variables and class variables together in Python

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles