Menu Close

Python class and instance variables

In this article, you will learn about what is Python class and instance variables using appropriate examples. If you learn Python object-oriented programming, Then you will always touch with class and instance variables.

At the end of this article, you are completely clear with class and instance variables in python.

To understand this article you should have basic knowledge of Python Programming. Python Object Oriented Programming allows variables on instance level and class level.

Let’s understand what is python class and instance variables.

Python class variables

Python Class variables define within class construction. Class variables are owned by the class itself, and class variables in Python are shared by all the instances or objects of the class.
Any instance or object accesses the class variable but can not be changed.

In Python, class variables are always defined outside of all methods.

Example:

class Tiger:
	type = "Tiger is an animal"
	def display(self):
		print(self.type)
			
obj1 = Tiger()
print(obj1.type)
obj1.display()
print("--------")
obj2 = Tiger()
print(obj2.type)
obj2.display()

Output

Tiger is an animal
Tiger is an animal
--------
Tiger is an animal
Tiger is an animal

Here we create two instances or objects of the Tiger class and access the class variable ‘type’ and method using the (.) dot symbol.

Here type is the class variable of the Tiger class because it defines within the class and the class variable only be the property of the class.

Python instance variables

The Python instance variables are different from the class variable because the class variable defines within the class but the instance variable defines within methods. The instance variable is owned by the instance or object of the class.

Example:

class Profile:		
    def __init__(self, name, age):
        self.myage = age
        self.myname = name
    def display(self):
	    print(f"my name is {self.myname} and my age is {self.myage}")

obj1 = Profile('John', 20)
obj1.display()


#access instance variable
print(obj1.myname)
print(obj1.myage)

Output

my name is John and my age is 20
John
20

You can check all the instance variables of an instance using the dir() function.

print(dir(obj1)

You can create an instance variable using an instance or object of the class. let’s create an instance variable address using obj1 object or instance.

Example:

class Profile:		
    def __init__(self, name, age):
        self.myage = age
        self.myname = name
    def display(self):
	    print(f"my name is {self.myname} and my age is {self.myage}")


obj1 = Profile('John', 20)
obj1.display()

#access instance varibale
print(obj1.myname)
print(obj1.myage)

#create instance variable address
obj1.address  = 'India'
print(obj1.address)

Output

my name is John and my age is 20
John
20
India

Conclusion

In this article, you have learned all about Python class and instance variables along with examples. This is the part of Python object-oriented programming.

If we talk about python class and instance variables, Then the python class variables associate with the class itself but the python instance variables associate with instance or object.

If you like this python class and instance variables article, please share it and keep visiting for further Python tutorials.

For more knowledge:- Click Here

FAQs About python class and instance variables

What are class and instance variables in Python?

Ans:- Class variables are variables that are associated with the class itself. Class variables create outside of the class and shared by all the instances of the class. You can’t change the class variable using the object of the class.

Does Python have class variables?

Ans:- Class variables in Python are defined within the class and the value of the class variable will be the same across all the instances of the class. Python class variables do not change using the instance or object of the class.

What are Python variables?

Ans:- A Python variable is a reserved memory location to store value for future usage.

Does Python have instance variables?

Ans:-Instance variable in python associated with object itself. Each instance has unique instance variables.

Python itertools Module Tutorial
Read CSV Files in Python

Related Posts