Earlier today i stumbled upon a behaviour with python classes which i could not fully comprehend.
If we take this piece of code:
class A:
a: str
When I test out the attribute access for a like so A.a or even A().a, I get an AttributeError: type object 'A' has no attribute 'a'.
On the contrary, if i just give A.a a value:
class A:
a: str = 'attribute a'
then i can access it with A.a. # returns "attribute a".
I do not fully understand why the first case raised an AttributeError.
In the first case, you are telling the type checker that there will be an attribute called
aof typestr, but you are not setting it.In the second case, you are actually giving
aa value