Sitemap
Emmanuel Nyadongo

Emmanuel Nyadongo

Press enter or click to view image in full size

Understanding Object and Class Attributes in Python

3 min readMar 17, 2023

In Python, classes and objects have attributes. Attributes are data members or variables that hold the state of the thing. In this post, we'll explore class attributes and instance attributes, how to create them, their differences, advantages and drawbacks, and how Python deals with them using the `__dict__` attribute.

Class Attributes
A class attribute is a variable that is shared by all class instances. It is defined within the class but outside any method. Class attributes are accessed using the class name followed by a dot (.) and the attribute name.

Here's an example:

class MyClass:
x = 5

print(MyClass.x) # 5

Instance Attributes An instance attribute is a variable that belongs to one specific example of a class. It is defined within a method (usually __init__) using the self keyword followed by a dot (.) and the attribute name.

Here's an example:

class MyClass:
def __init__(self):
self.x = 5

obj = MyClass()
print(obj.x) # 5

Creating Class and Instance Attributes There are several ways to generate class and instance attributes in Python. The most common way to create an instance attribute is by defining it within the __init__ method using self.attribute_name. The most common way to create a class attribute is by defining it within the class but outside any process.

Get Emmanuel Nyadongo’s stories in your inbox

Join Medium for free to get updates from this writer.

Here's an example that shows both ways:

class MyClass:
x = 5 # class attribute

def __init__(self):
self.y = 10 # instance attribute

obj = MyClass()
print(MyClass.x) # 5
print(obj.y) # 10

Differences Between Class and Instance Attributes The main difference between class and instance attributes is their scope. All class instances share class attributes, while instance attributes belong to one specific example.

Another difference is how they are accessed. Class attributes are accessed using the class name, while instance attributes are accessed using the object reference.

Advantages and Drawbacks Class attributes can be helpful when you want to share data between all class instances. However, this can also be a drawback if you need each model to have its value for that attribute.

Instance attributes allow each object to have its unique value for that attribute but can use more memory if there are many instances with different values.

How Python Deals with Object and Class Attributes Using __dict__ In Python, every object has a built-in dictionary called __dict__, which stores its attributes. When you access a feature on an object, Python first looks in its __dict__ dictionary for that key. If it doesn't find it there, it seems in its superclass' __dict__, then its superclass' superclass’ `dict, etc., until it finds it or reaches the top of the inheritance chain.

Here's an example:

class MyClass:
x = 5

obj = MyClass()
obj.y = 10

print(obj.__dict__) # {'y': 10}
print(MyClass.__dict__) # {... 'x': 5 ...}

In conclusion, understanding how object and class attribute work in Python can help you write more efficient code.

--

--