Skip to content

Instantly share code, notes, and snippets.

@xhiroga
Created November 20, 2019 00:26
Show Gist options
  • Save xhiroga/6c5a8ac4749d25f5ecbffcfad9e750e4 to your computer and use it in GitHub Desktop.
Save xhiroga/6c5a8ac4749d25f5ecbffcfad9e750e4 to your computer and use it in GitHub Desktop.
Pythonでdaataclassのうち、値がNoneの項目を除外して辞書化するスニペット
dataclasses.asdict(
obj,
dict_factory=lambda tuples: {tuple[0]: tuple[1] for tuple in tuples if tuple[1] is not None}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment