Classes as well as their instances in python uses dictionary like data structure to store the information.

So for each class definition, a dictionary will be allocated where the class level information (class variables) will be stored. And for each instance of that particular class, a separate dictionary(self) will be allocated where the instance specific information(instance variables) will be stored.

So now the next question is: How the lookup for a particular name will be performed ??

And answer to this question is that if you are accessing the names through some instance, the instance specific dictionary will be searched first and if the name is not found there, then the class dictionary will be searched for that name. So if the same value is defined at both levels that former one will be overridden.

Note that when you write d['key'] = val where d is a dictionary, 'key' will automatically be added to the dictionary if not already present. Otherwise the current value will be overwritten. Keep this in mind before reading the further explanation.

Now lets go through the code you have used to describe your problem:

MyClass1

class MyClass1:
    q=1
    def __init__(self,p):
        self.p=p
    def AddSomething(self,x):
        self.q = self.q+x

1. my = Myclass1(2) #create new instance and add variables to it.

    MyClass = {"q" : 1}
    my = {"p" : 2}

2. my.p    # =2, p will be taken from Dictionary of my-instance.

3. my.q    # =1, q will be takn from MyClass dict. (Not present in dictionary of my-instance).

4. my.AddSomething(7) # This method access the value of q (using self.q) first 
                      # which is not defined in my dict and hence will be taken
                      # from dictionary of MyClass. After the addition operation,
                      # the sum is being stored in self.q. Note that now we are
                      # adding the name q to Dictionary of my-instance and hence                   
                      # a new memory space will be created in Dictionary of my-instance
                      # and the future references to self.q will fetch the value
                      # of self.q from dictionary of my-instance.

    MyClass = {"q" : 1}
    my = {"p" : 2, "q" : 8}

5. my.q   # =8, q now is available in dictionary of my-instance.
Answer from Shasha99 on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
Class objects support two kinds of operations: attribute references and instantiation. Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.
Discussions

python - Class attributes and their initialization - Stack Overflow
I'm quite new in python and during these days I'm exploring classes. I have a question concerning attributes and variables inside classes: What is the difference between defining an attribute via j... More on stackoverflow.com
🌐 stackoverflow.com
python - Getting attributes of a class - Stack Overflow
I want to get the attributes of a class, say: class MyClass(): a = "12" b = "34" def myfunc(self): return self.a using MyClass.__dict__ gives me a list of attributes and functions, and ... More on stackoverflow.com
🌐 stackoverflow.com
Any way to get ALL attributes of an object in python?
movieobject.__dict__ will give you al the attributes and their values movieobject.__dict__.keys() will give you only the names of the attributes. More on reddit.com
🌐 r/learnpython
10
9
June 18, 2024
How to avoid inheriting all attributes
No, this goes against the whole principle of inheritance. If you need Bar to not have attributes that are defined in Foo, then you should not make it inherit from Foo but get them both to have a common superclass that only defines the things they have in common. More on reddit.com
🌐 r/learnpython
23
15
May 17, 2024
People also ask

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: An Overly Thorough Guide | Toptal®
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: An Overly Thorough Guide | 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: An Overly Thorough Guide | Toptal®
🌐
Built In
builtin.com › software-engineering-perspectives › python-attributes
Python Attributes: Class vs. Instance | Built In
Python attributes are variables or methods associated with an object that store data about the object's properties and behavior. Class attributes belong to a class, while instance attributes belong to a specific object and are unique to each object.
🌐
Bruceeckel
bruceeckel.com › 2022 › 05 › 11 › misunderstanding-python-class-attributes
Misunderstanding Python Class Attributes
May 11, 2022 - In class B, x is changed to a static variable, which means there is only a single piece of x storage for the class—no matter how many instances of that class you create. This is how Python class attributes work; they are static variables without using the static keyword.
Find elsewhere
🌐
Toptal
toptal.com › developers › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
January 16, 2026 - When you try to access Python attributes from an instance of a class, it first looks at its instance namespace. If it finds the attribute, it returns the associated value. If not, it then looks in the class namespace and returns the attribute (if it’s present, otherwise throwing an error).
🌐
Tutorialspoint
tutorialspoint.com › python › python_class_attributes.htm
Python - Class Attributes
The properties or variables defined inside a class are called as Attributes. An attribute provides information about the type of data a class contains. There are two types of attributes in Python namely instance attribute and class attribute.
🌐
GeeksforGeeks
geeksforgeeks.org › python › class-instance-attributes-python
Class and Instance Attributes in Python - GeeksforGeeks
Such attributes are defined in the class body parts usually at the top, for legibility. ... # Write Python code here class sampleclass: count = 0 # class attribute def increase(self): sampleclass.count += 1 # Calling increase() on an object s1 = sampleclass() s1.increase() print(s1.count) # Calling increase on one more # object s2 = sampleclass() s2.increase() print(s2.count) print(sampleclass.count)
Published   September 5, 2024
🌐
TutorialsPoint
tutorialspoint.com › built-in-class-attributes-in-python
Built-In Class Attributes in Python
In this article, we will explain to you the built-in class attributes in python The built-in class attributes provide us with information about the class. Using the dot (.) operator, we may access the built-in class attributes. The built-in class att
Top answer
1 of 3
18

Classes as well as their instances in python uses dictionary like data structure to store the information.

So for each class definition, a dictionary will be allocated where the class level information (class variables) will be stored. And for each instance of that particular class, a separate dictionary(self) will be allocated where the instance specific information(instance variables) will be stored.

So now the next question is: How the lookup for a particular name will be performed ??

And answer to this question is that if you are accessing the names through some instance, the instance specific dictionary will be searched first and if the name is not found there, then the class dictionary will be searched for that name. So if the same value is defined at both levels that former one will be overridden.

Note that when you write d['key'] = val where d is a dictionary, 'key' will automatically be added to the dictionary if not already present. Otherwise the current value will be overwritten. Keep this in mind before reading the further explanation.

Now lets go through the code you have used to describe your problem:

MyClass1

class MyClass1:
    q=1
    def __init__(self,p):
        self.p=p
    def AddSomething(self,x):
        self.q = self.q+x

1. my = Myclass1(2) #create new instance and add variables to it.

    MyClass = {"q" : 1}
    my = {"p" : 2}

2. my.p    # =2, p will be taken from Dictionary of my-instance.

3. my.q    # =1, q will be takn from MyClass dict. (Not present in dictionary of my-instance).

4. my.AddSomething(7) # This method access the value of q (using self.q) first 
                      # which is not defined in my dict and hence will be taken
                      # from dictionary of MyClass. After the addition operation,
                      # the sum is being stored in self.q. Note that now we are
                      # adding the name q to Dictionary of my-instance and hence                   
                      # a new memory space will be created in Dictionary of my-instance
                      # and the future references to self.q will fetch the value
                      # of self.q from dictionary of my-instance.

    MyClass = {"q" : 1}
    my = {"p" : 2, "q" : 8}

5. my.q   # =8, q now is available in dictionary of my-instance.
2 of 3
5

q=1 inside the class is a class attribute, associated with the class as a whole and not any particular instance of the class. It is most clearly accessed using the class itself: MyClass1.q.

A instance attribute is assigned directly to an instance of a class, usually in __init__ by assigning to self (such as with self.p = p), but you can assign attributes to an instance at any time.

Class attributes can be read either using the class binding (MyClass.q) or an instance binding (my.q, assuming it is not shadowed by an instance attribute with the same name). They can only be set, however, using a class binding. Setting a value with an instance binding always modifies an instance attribute, creating it if necessary. Consider this example:

>>> a = MyClass1()
>>> a.q
1
>>> a.q = 3    # Create an instance attribute that shadows the class attribute
3
>>> MyClass1.q
1
>>> b = MyClass1()
>>> b.q   # b doesn't have an instance attribute q, so access the class's
1
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.3 documentation
The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. Three attributes on exception objects provide information about the context in which the exception was raised:
🌐
Real Python
realpython.com › lessons › class-and-instance-attributes
Class and Instance Attributes (Video) – Real Python
We make a distinction between instance attributes and class attributes. 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: Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Published   March 22, 2019
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
And each attribute has a type. Then you create an instance of that class with some values and it will validate the values, convert them to the appropriate type (if that's the case) and give you an object with all the data.
🌐
W3Schools
w3schools.com › python › python_class_properties.asp
Python Class Properties
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... Properties are variables that belong to a class....
🌐
scikit-learn
scikit-learn.org › stable › modules › svm.html
1.4. Support Vector Machines — scikit-learn 1.8.0 documentation
A low C makes the decision surface smooth, while a high C aims at classifying all training examples correctly. gamma defines how much influence a single training example has. The larger gamma is, the closer other examples must be to be affected. Proper choice of C and gamma is critical to the SVM’s performance. One is advised to use GridSearchCV with C and gamma spaced exponentially far apart to choose good values. ... You can define your own kernels by either giving the kernel as a python function or by precomputing the Gram matrix.
🌐
freeCodeCamp
freecodecamp.org › news › python-attributes-class-and-instance-attribute-examples
Python Attributes – Class and Instance Attribute Examples
April 12, 2022 - 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › built-in-class-attributes-in-python
Built-In Class Attributes In Python - GeeksforGeeks
July 23, 2025 - In conclusion, built-in class attributes in Python, such as doc, name, module, bases, and dict, serve as invaluable tools for understanding and working with classes. These attributes offer metadata about class structures, documentation, and ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.html
pandas.DataFrame — pandas 3.0.1 documentation
Two-dimensional, size-mutable, potentially heterogeneous tabular data · Data structure also contains labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure
🌐
Python Morsels
pythonmorsels.com › using-class-attributes
Using attributes on classes - Python Morsels
July 31, 2023 - Class attributes should be used for a state that should be updated on every instance of a class, not just on one instance. Although, this is honestly pretty uncommon. It's unusual to see classes that use shared state that changes over time. Classes are objects in Python (everything is an object), and like all objects, they can have attributes.