Last active
September 2, 2019 01:51
-
-
Save lakinwecker/c653f464f3e330b97e2d8f7022979433 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dataclasses | |
import collections | |
import typing | |
@dataclasses.dataclass | |
class Foo: | |
d: typing.DefaultDict[str, int] = dataclasses.field( | |
default_factory=lambda: collections.defaultdict(int)) | |
f = Foo() | |
print(f.d["asdf"]) | |
print(dataclasses.asdict(f)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "dc.py", line 12, in <module> | |
print(dataclasses.asdict(f)) | |
File "/usr/lib/python3.7/dataclasses.py", line 1044, in asdict | |
return _asdict_inner(obj, dict_factory) | |
File "/usr/lib/python3.7/dataclasses.py", line 1051, in _asdict_inner | |
value = _asdict_inner(getattr(obj, f.name), dict_factory) | |
File "/usr/lib/python3.7/dataclasses.py", line 1083, in _asdict_inner | |
for k, v in obj.items()) | |
TypeError: first argument must be callable or None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment