Introduction
Welcome to my 66th blog post on the Python journey. In Python, variables can be defined at instance and on class level. Knowing this concept can help write code in a more maintainable and efficient way. On day 66, I learned about instance variables and class variables in Python. Let's dive into more details and understand these variables in Python.
So let's get started......
Class Variables
They are defined at a class and are shared with all the new instances created for that class.
They are used to store information that is common to all instances of the classes.
Class variables are defined outside of any method.
Example -
In the below code, class_variable is defined at the class level and can be used by any instance of the Myclass. When we create new instances of MyClass, the value of class_variable is incremented.
class MyClass:
class_variable = 0
def __init__(self):
MyClass.class_variable += 1
def print_class_variable(self):
print(MyClass.class_variable)
obj1 = MyClass()
obj2 = MyClass()
obj1.print_class_variable() # Output: 2
obj2.print_class_variable() # Output: 2
#MyClass.print_class_variable(obj2) #above line can also be written as this
Instance Variables
They are defined at the instance level and are unique to each instance of the class.
Are defined inside the init method i.e the constructor of the class
Instance Variables are usually used to store information that is specific to each instance of the class.
Example -
An instance variable can be used to store the name of an employee in a class that represents an employee.
class MyClass:
def __init__(self, name):
self.name = name
def print_name(self):
print(self.name)
obj1 = MyClass("John")
obj2 = MyClass("Jane")
obj1.print_name() # Output: John
obj2.print_name() # Output: Jane
In the above example, 2 different objects are created with obj1 and obj2 and 2 different instances of name are created of the class MyClass.
More Example
class Employee:
companyName = "Apple"
def __init__(self, name):
self.name = name
self.raise_amount = 0.02
def showDetails(self):
print(f"The name of the Employee is {self.name} and the raise amount in {self.companyName} is {self.raise_amount}")
# Employee.showDetails(emp1)
emp1 = Employee("Harry")
emp1.raise_amount = 0.3
emp1.companyName = "Apple India"
emp1.showDetails()
Employee.companyName = "Google"
print(Employee.companyName)
emp2 = Employee("Rohan")
emp2.companyName = "Nestle"
emp2.showDetails()
Resources Used
You can watch the video of Day#66 by clicking on the below link πππππ
Conclusion
Thanks, guys for going through this blog post. On day 66, I learned about instance and class variables and how they can be used to store information on the instance and class level in Python respectively.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #66
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.