Feb-21-2022, 02:02 PM
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
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:
Output:
Based on this code snippet, my understanding is that the declared first
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.
__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:
1 2 3 4 5 6 7 8 |
class MyClass( object ): i = 123 def __init__( self ): self .i = 345 a = MyClass() print (MyClass.i) print (a.i) |
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.