[issue43953] InitVar should not be available on a @dataclass-decorated class

Eric V. Smith report at bugs.python.org
Tue Apr 27 08:46:28 EDT 2021


Eric V. Smith <eric at trueblade.com> added the comment:

This is just how Python works. The default values for .x and .y are set on the class A, not its instances. Here's the equivalent without dataclasses:

>>> class A:
...   x = 0
...   y = 1
...
>>> a = A()
>>> a.x
0
>>> a.y
1
>>> a.__dict__
{}
>>> A.__dict__
mappingproxy({'__module__': '__main__', 'x': 0, 'y': 1, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})

Note that the instance "a" has nothing in its __dict__, but the values A.x and A.y are none the less available through the instance "a".

The only way to avoid this would be for @dataclass to delete the values from A, but that's not a change I'd be willing to make.

Is this causing some actual problem in any code?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43953>
_______________________________________


More information about the Python-bugs-list mailing list