I want to make sure that the instance dict is being initialized when I think it should be.
In:
@dataclass
class C:
a: int
b: list = field(default_factory=list, init=False)
c: list = field(default_factory=list)
d: int = field(default=4, init=False)
e: int = 0
c=C(0)
Then:
only a, b, c, and e should be in the instance dict
d should be in the class dict with value 4
e should be in the class dict with value 0
The reasoning is:
a is in the instance dict because it's assigned to in __init__.
b is in the instance dict, even though it's not in the __init__ param list. This is because the default factory still needs to be called from __init__.
c is in the instance dict because it's assigned to in __init__.
d is not in the instance dict because it's not assigned to by __init__. It's in the class dict because it has a default value.
e is in the instance dict because it's assigned to in __init__.
The generated __init__ looks like:
def __init__(self,a:_type_a,c:_type_c=_MISSING,e:_type_e=_dflt_e)->_return_type:
self.a = a
self.b = _dflt_b()
self.c = _dflt_c() if c is _MISSING else c
self.e = e
locals: {'_type_a': <class 'int'>, '_type_b': <class 'list'>, '_type_c': <class 'list'>, '_type_d': <class 'int'>, '_type_e': <class 'int'>, '_return_type': None}
globals: {'_MISSING': <object object at 0x10ea26100>, '_dflt_b': <class 'list'>, '_dflt_c': <class 'list'>, '_dflt_e': 0}
I want to make sure that the instance dict is being initialized when I think it should be.
In:
Then:
The reasoning is:
__init__
.__init__
param list. This is because the default factory still needs to be called from__init__
.__init__
.__init__
. It's in the class dict because it has a default value.__init__
.The generated
__init__
looks like: