Python Forum
Distinguishing different types of class attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Distinguishing different types of class attributes
#1
I’m learning Python OOP and I am struggling to understand the difference between defining the two different kinds of class attributes - - static attributes (declared above the dunder __init__ method) as compared to instance attributes (declared beneath and within the scope of the __init__ method).

When I search Google for ‘python difference dunder init method namespace’ the results are split between guides and SO questions/answers explaining either the __init__.py files for Python packaging or the basics on how to instantiate classes in general. Another one here.

I can’t seem to locate a guide which directly addresses the comparison between the different types of the two attributes.

With the above search terms (and some variations), one of the recurring top results is the most useless document of all - - the entry on __init__ objects from the official Python website. I say ‘useless’ because the official doc is practically written in a foreign language (by programmers, for programmers).

What I am looking for is a casual, candid clarification in plain english with practical examples and a description of good potential use cases.

The closest I came to finding an answer to my original question is from SO. In that SO answer, here is a code snippet with its output after execution:

class MyClass(object):
    i = 123
    def __init__(self):
        self.i = 345
  
a = MyClass()
print(MyClass.i)
print(a.i)
Output:
Quote:123
345

Based on this code snippet, my understanding is that the declared first i attribute is universal and applies every time the class is called, whereas the second i also applies at run time, but is just a default container until the program at run time changes it to something else. Is this accurate? Could someone illustrate and better clarify the difference / distinction between the two different types of class attributes? Perhaps also sharing some meaningful use cases for both would be helpful too.

The larger question I seek an answer to is when (and in what context) would a programmer use one of the two possible attribute types instead of the other?

I came up with this question while watching Fred Baptiste’s Python course on OOP.
Reply


Messages In This Thread
Distinguishing different types of class attributes - by Drone4four - Feb-21-2022, 02:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 6 606 Apr-14-2025, 07:02 AM
Last Post: buran
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,842 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Variable Types in a class nafshar 9 4,615 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,890 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Calls to Attributes of a Class SKarimi 3 4,566 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  SQL Alchemy dynamic class - declarative_base losing attributes mrdominikku 4 4,953 Jan-10-2020, 06:46 PM
Last Post: mrdominikku
  OpenCV - Distinguishing between three icons kainev 2 2,662 Jul-30-2019, 11:57 PM
Last Post: kainev
  how to add class instance attributes from list 999masks 2 3,329 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Is it possible to loop through class attributes via string? 04chiak 3 11,227 Feb-04-2018, 09:29 PM
Last Post: 04chiak
  Class Instances overriding class members and attributes. Leaf 7 10,719 Nov-29-2017, 06:08 AM
Last Post: Leaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020