This is because of the way Python resolves names with the .. When you write self.list the Python runtime tries to resolve the list name first by looking for it in the instance object, and if it is not found there, then in the class instance.

Let's look into it step by step

self.list.append(1)
  1. Is there a list name into the object self?
    • Yes: Use it! Finish.
    • No: Go to 2.
  2. Is there a list name into the class instance of object self?
    • Yes: Use it! Finish
    • No: Error!

But when you bind a name things are different:

self.list = []
  1. Is there a list name into the object self?
    • Yes: Overwrite it!
    • No: Bind it!

So, that is always an instance variable.

Your first example creates a list into the class instance, as this is the active scope at the time (no self anywhere). But your second example creates a list explicitly in the scope of self.

More interesting would be the example:

class testClass():
    list = ['foo']
    def __init__(self):
        self.list = []
        self.list.append('thing')

x = testClass()
print x.list
print testClass.list
del x.list
print x.list

That will print:

['thing']
['foo']
['foo']

The moment you delete the instance name the class name is visible through the self reference.

Answer from rodrigo on Stack Overflow
Top answer
1 of 4
65

This is because of the way Python resolves names with the .. When you write self.list the Python runtime tries to resolve the list name first by looking for it in the instance object, and if it is not found there, then in the class instance.

Let's look into it step by step

self.list.append(1)
  1. Is there a list name into the object self?
    • Yes: Use it! Finish.
    • No: Go to 2.
  2. Is there a list name into the class instance of object self?
    • Yes: Use it! Finish
    • No: Error!

But when you bind a name things are different:

self.list = []
  1. Is there a list name into the object self?
    • Yes: Overwrite it!
    • No: Bind it!

So, that is always an instance variable.

Your first example creates a list into the class instance, as this is the active scope at the time (no self anywhere). But your second example creates a list explicitly in the scope of self.

More interesting would be the example:

class testClass():
    list = ['foo']
    def __init__(self):
        self.list = []
        self.list.append('thing')

x = testClass()
print x.list
print testClass.list
del x.list
print x.list

That will print:

['thing']
['foo']
['foo']

The moment you delete the instance name the class name is visible through the self reference.

2 of 4
10

Python has interesting rules about looking up names. If you really want to bend your mind, try this code:

class testClass():
    l = []
    def __init__(self):
        self.l = ['fred']

This will give each instance a variable called l that masks the class variable l. You will still be able to get at the class variable if you do self.__class__.l.

The way I think of it is this... Whenever you do instance.variable (even for method names, they're just variables who's values happen to be functions) it looks it up in the instance's dictionary. And if it can't find it there, it tries to look it up in the instance's class' dictionary. This is only if the variable is being 'read'. If it's being assigned to, it always creates a new entry in the instance dictionary.

🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation. In C++ terminology, normally class members (including the data members) are public (except see below Private Variables), and all member functions ...
People also ask

What is a Python namespace?

A Python namespace is a mapping from names to objects, with the property that there is zero relation between names in different namespaces. Namespaces are usually implemented as Python dictionaries, although this is abstracted away.

🌐
toptal.com
toptal.com › developers › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: Examples of Variables | Toptal®
Python class method versus instance method: What’s the difference?

In Python, a class method is a method that is invoked with the class as the context. This is often called a static method in other programming languages. An instance method, on the other hand, is invoked with an instance as the context.

🌐
toptal.com
toptal.com › developers › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: Examples of Variables | Toptal®
What happens if both instance attribute and class attribute are defined?

In that case, the instance namespace takes precedence over the class namespace. If there is an attribute with the same name in both, the instance namespace will be checked first and its value returned.

🌐
toptal.com
toptal.com › developers › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: Examples of Variables | Toptal®
🌐
Toptal
toptal.com › developers › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: Examples of Variables | Toptal®
January 16, 2026 - If a Python class variable is set by accessing an instance, it will override the value only for that instance. This essentially overrides the class variable and turns it into an instance variable available intuitively only for that instance.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-class-and-instance-variables-in-python-3
Understanding Class and Instance Variables in Python 3 | DigitalOcean
August 20, 2021 - Defined outside of all the methods, class variables are, by convention, typically placed right below the class header and before the constructor method and other methods. Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command.
🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-34-class-or-static-variables-in-python
Class (Static) and Instance Variables in Python - GeeksforGeeks
3 weeks ago - Class variables are shared by all objects of a class, whereas instance variables are unique to each object. Unlike languages such as Java or C++, Python does not require a static keyword.
Find elsewhere
🌐
Medium
medium.com › analytics-vidhya › are-you-not-sure-where-to-use-class-variables-in-python-cce0af8f514d
Are you not sure where to use class variables in python? | by Kasmitharam | Analytics Vidhya | Medium
February 7, 2024 - And hence it cannot be called via an attribute similar to other instance variables such as pay, first, and last name. The second problem is that if the raise amount(1.04) is mentioned in multiple places, we have to make changes in more than one place which is kind of manually updating stuff. So in order to overcome these scenarios we use class variables.
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Class names should normally use the CapWords convention. The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable. Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants. Names of type variables ...
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 month ago - This syntax indicates that the class LoggedVar is parameterised around a single type variable T . This also makes T valid as a type within the class body. Generic classes implicitly inherit from Generic.
🌐
PYnative
pynative.com › home › python › python object-oriented programming (oop) › python class variables
Python Class Variables With Examples – PYnative
September 8, 2023 - Class Variables: A class variable is a variable that is declared inside of a class but outside of any instance method or init() method. Class variables are shared by all instances of a class. Read More: Instance variables in Python with Examples
🌐
Digis
digiscorp.com › understanding-python-class-variables-a-beginners-guide
Understanding Python Class Variables: A Beginner's Guide
July 22, 2025 - If you assign a new value to a class variable using an instance, Python will create an instance variable instead — shadowing the class variable.
🌐
DEV Community
dev.to › ankitmalikg › difference-bw-class-variable-and-instance-variable-python-100o
Python - Difference b/w class variable and instance variable - DEV Community
August 31, 2024 - Instance variables, on the other hand, are variables that are unique to each instance of a class. They are created when an instance of the class is created and they are associated with that specific instance. To declare an instance variable, you need to define it inside a method or constructor of the class.
🌐
Pydantic
docs.pydantic.dev › latest › concepts › models
Models - Pydantic Validation
However, if constraints or a default value (as per PEP 696) is being used, then the default type or constraints will be used for both validation and serialization if the type variable is not parametrized. You can override this behavior using SerializeAsAny: Python 3.9 and abovePython 3.13 and above · from typing import Generic from typing_extensions import TypeVar from pydantic import BaseModel, SerializeAsAny class ErrorDetails(BaseModel): foo: str ErrorDataT = TypeVar('ErrorDataT', default=ErrorDetails) class Error(BaseModel, Generic[ErrorDataT]): message: str details: ErrorDataT class MyEr
🌐
Ttu
ttu.github.io › python-class-instance-variables
Python Class and Instance Variables
July 14, 2022 - In particular, the value-less notation a: int allows one to annotate instance variables that should be initialized in init or new. The proposed syntax is as follows: ... from typing import ClassVar class Starship: captain: str = "Picard" # instance variable with default damage: int # instance variable without default stats: ClassVar[dict[str, int]] = {} # class variable def __init__(self, damage: int, captain: str | None = None): self.damage = damage if captain: self.captain = captain # Else keep the default def hit(self) -> None: Starship.stats['hits'] = Starship.stats.get('hits', 0) + 1 # No
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-class-members
Python Class Members - GeeksforGeeks
To understand Class members, let ... work: Instance variables in python are such that when defined in the parent class of the object are instantiated and separately maintained for each and every object instance of the class...
Published   April 17, 2023
🌐
CBT Nuggets
cbtnuggets.com › blog › technology › programming › python-class-variables-explained
Python Class Variables: Explained
Here is a good way to sum up the relationship: a class variables is shared by all objects that are created. An instance of the class variable is always created on each newly minted object; it overrides the class instance. Lastly, an instance variable is only accessible to the object it was defined in. Warning: The following code will only work in Python 3.x
🌐
Google
developers.google.com › google for education › python
Google's Python Class | Python Education | Google for Developers
The first exercises work on basic Python concepts like strings and lists, building up to the later exercises which are full programs dealing with text files, processes, and http connections. The class is geared for people who have a little bit of programming experience in some language, enough to know what a "variable" or "if statement" is.
🌐
Python
docs.python.org › 3 › library › dataclasses.html
dataclasses — Data Classes
February 23, 2026 - Python stores default member variable values in class attributes.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-classes-and-objects
Python Classes and Objects - GeeksforGeeks
__str__ Implementation: Defined as a method in Dog class. Uses self parameter to access instance's attributes (name and age). Readable Output: When print(dog1) is called, Python automatically uses __str__ method to get a string representation of object. Without __str__, calling print(dog1) would produce something like <__main__.Dog object at 0x00000123>. In Python, variables defined in a class can be either class variables or instance variables and understanding distinction between them is crucial for object-oriented programming.
Published   1 week ago