Instance Attribute in Python
An instance attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object, and it’s defined inside the constructor function, __init__(self,..) of the class.
A class attribute is a Python variable that belongs to a class rather than a particular object. It’s shared between all the objects of this class and is defined outside the constructor function, __init__(self,...), of the class.
The below ExampleClass is a basic Python class with two attributes: class_attr and instance_attr.
class ExampleClass(object):
class_attr = 0
def __init__(self, instance_attr):
self.instance_attr = instance_attrThe instance_attr is only accessible from the scope of an object. The class attribute (class_attr) is accessible as both a property of the class and as a property of objects, as it’s shared between all of them.
Instance Attributes are unique to each object, (an instance is another name for an object). Here, any Dog object we create will be able to store its name and age. We can change either attribute of either dog, without affecting any other dog objects we’ve created.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = ageThis __init__ is called the initializer. It is automatically called when we instantiate the class. It’s job is to make sure the class has any attributes it needs. It’s sometimes also used to make sure that the object is in a valid state when it’s instantiated, like making sure the user didn’t enter a negative age for the dog.
We have to include the self parameter so that our initializer has a reference to the new object being instantiated.
Class Attributes are unique to each class. Each instance of the class will have this attribute. It’s sometimes used to specify a defualt value that all objects should have after they’ve been instantiated. Here, our class attribute is species
class Dog:
species = 'mammal'
def __init__(self, name, age):
self.name = name
self.age = ageUnlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy). To list the attributes of an instance/object, we have two functions:- 1. vars()– This function displays the attribute of an instance in the form of an dictionary. 2. dir()– This function displays more attributes than vars function,as it is not limited to instance. It displays the class attributes as well. It also displays the attributes of its ancestor classes.
# Python program to demonstrate
# instance attributes.
class emp:
def __init__(self):
self.name = 'xyz'
self.salary = 4000
def show(self):
print(self.name)
print(self.salary)
e1 = emp()
print("Dictionary form :", vars(e1))
print(dir(e1))Output:
Dictionary form :{'salary': 4000, 'name': 'xyz'}
['__doc__', '__init__', '__module__', 'name', 'salary', 'show']When creating a class in Python, you'll usually create attributes that may be shared across every object of a class or attributes that will be unique to each object of the class.
In this article, we’ll see the difference between class attributes and instance attributes in Python with examples.
To give a basic definition of both terms, class attributes are class variables that are inherited by every object of a class. The value of class attributes remain the same for every new object.
Get Vipul Kaitke’s stories in your inbox
Join Medium for free to get updates from this writer.
Like you will see in the examples in this section, class attributes are defined outside the __init__() function.
On the other hand, instance attributes, which are defined in the __init__() function, are class variables that allow us to define different values for each object of a class.
Here is an example:
class Student:
school = "VEVCodelab"
def __init__(self, name, course):
self.name = name
self.course = course
Student1 = Student("pqr", "Java")
Student2 = Student("xyz", "Python")
print(Student1.name)
print(Student2.name) In the code above, we created a variable in the Student class called school.
We created two more variables but in the __init__() function – name and course – which we initialized using the self parameter.
The first parameter in an __init__() function is used to initialize other parameters when creating variables in the function. You can call it whatever you want – by convention, self is mostly used.
The school variable acts as a class attribute while name and course are instance attributes. Let's break the example above down to explain instance attributes.
Student1 = Student("PQR", "Java")
Student2 = Student("XYZ", "Python")
print(Student1.name)
print(Student2.name) We created two objects from the Student class – Student1 and Student2. Each of these objects, by default, will have all the variables created in the class. But each object is able to have its own name and course variable because they were created in the __init__() function.
Now let’s print the school variable for each object and see what happens.
print(Student1.school) # VEVCodelab
print(Student2.school) # VEVCodelabBoth gave us the same value because the school variable is a class attribute.
In summary, class attributes remain the same for every object and are defined outside the __init__() function. Instance attributes are somewhat dynamic because they can have different values in each object.
Instance attributes are defined in the __init__() function.
Happy coding!







