In this article we want to learn about Difference Between Python Class & Instance Attributes, so first of all let’s talk about Python Instance Attributes.
What is Python Instance Attribute ?
Python instance attribute is a variable belonging to only one object. This variable is only accessible in the scope of this object and it is defined inside the init() method also Instance Attributes are unique to each object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Define a class named Student class Student: # Initialize the class with name and email attributes def __init__(self, name, email): self.name = name self.email = email # Create an instance of the Student class p1 = Student("Codeloop", "codeloop@gmail.com") # Print the name and email attributes of the instance p1 print("Name is: ", p1.name) print("Email Is: ", p1.email) # Create another instance of the Student class p2 = Student("Parwiz", 'par@gmail.com') # Print the name and email attributes of the instance p2 print("Name is: ", p2.name) print("Email Is: ", p2.email) |
In this example the name and email are instance attributes, because it is in the init() method, and these variables are unique to each object. you can see that we have created two objects of Student class, and the instance variables are unique for each object, if you change one of them , there will be no affect in the other object.
Run the code and this will be the result.
What is Python Class Attribute ?
Python class attribute is a variable that is owned by a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the initializer method of the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Define a class named Student class Student: age = 20 # Initialize the class with name and email attributes def __init__(self, name, email): self.name = name self.email = email # Create an instance of the Student class p1 = Student("Codeloop", "codeloop@gmail.com") # Print the name and email attributes of the instance p1 print("Name is: ", p1.name) print("Email Is: ", p1.email) # Print the class-level attribute age print(p1.age) # Create another instance of the Student class p2 = Student("Parwiz", 'par@gmail.com') # Print the name and email attributes of the instance p2 print("Name is: ", p2.name) print("Email Is: ", p2.email) # Print the class-level attribute age print(p2.age) |
in above examples the age is a class attribute and it belongs to the class, it is shared by all instances. when you change the value of a class attribute, it will affect all instances that share the same exact value.
This will be the result
FAQs:
Difference between class and instance variable in Python
- Class Variable:
- A class variable is a variable that is shared among all instances (objects) of a class.
- It is defined inside the class definition but outside of any instance methods.
- Class variables are accessed using the class name itself, followed by the dot operator.
- Changes made to a class variable affect all instances of the class.
- Instance Variable:
- An instance variable is a variable that is unique to each instance (object) of a class.
- It is defined within the constructor method (__init__) using the self keyword.
- Instance variables are accessed using the instance name followed by the dot operator.
- Each instance maintains its own copy of instance variables, and changes made to them only affect that specific instance.
Difference between class and attribute:
- Class:
- A class is a blueprint for creating objects (instances) that define properties and behaviors.
- It acts as a template that encapsulates data and methods related to the objects it creates.
- Classes are defined using the class keyword in Python.
- Attribute:
- An attribute is a piece of data associated with a class or its instances (objects).
- It can be either a class attribute or an instance attribute.
- Class attributes are shared among all instances of the class, while instance attributes are unique to each instance.
- Attributes can include variables, methods, properties, etc.
Learn More:
Subscribe and Get Free Video Courses & Articles in your Email