Fundamentals 6 min read

Understanding Python Class vs Instance Variables: A Clear Guide

This article explains the difference between class variables and instance variables in Python, demonstrates how they behave during object instantiation, and shows how modifying each affects other objects, providing clear examples and visual illustrations for learners.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Understanding Python Class vs Instance Variables: A Clear Guide

Introduction

Hello, I am a Python enthusiast. Recently a follower asked a question about Python class variables and instance variables in an English‑language discussion group, so I am sharing the answer here for everyone to learn together.

Solution Process

The answer involves three parts: class attributes, instance attributes, and the reference‑resolution behavior of instance attributes. In the example, counter is a class attribute and __first is an instance attribute. The print statement shows how the instance attribute reference works.

When ExampleClass is instantiated, the __init__ magic method runs the code below it, which manipulates both kinds of attributes. Instance attributes belong only to the specific object and appear after instantiation; each object has its own independent instance attributes, as shown by the .__dict__ output. Class attributes exist as soon as the class is defined; they can be accessed via Class.attribute and modified, and they are shared by all instances. In this example, each time the class is instantiated ExampleClass.counter increments by 1, so the three print calls all show the same value.

Additional extension: by inserting self.counter += 1 before ExampleClass.counter += 1 in __init__, a new instance attribute counter is created. The .__dict__ now shows this instance attribute, and self.counter refers to it rather than the class attribute. The reason self.counter += 1 works is that the lookup first finds the instance attribute; if it does not exist, it falls back to the class attribute. During the first instantiation the class attribute counter is 0, so adding 1 assigns 1 to the new instance attribute. In the second instantiation the class attribute has already been incremented to 1, so the new instance attribute becomes 2, and so on.

Summary

This article, based on a follower's question, provides a detailed explanation and demonstration of Python class variables and instance variables, helping the asker resolve the issue.

In short, class variables are shared across all instances of a class, while instance variables are unique to each instantiated object.

The key difference is that modifying a class variable changes the value for every object, whereas changing an instance variable affects only that particular object.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

fundamentalsobject‑oriented programmingClass Variablesinstance variables
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.