Skip to content

Instantly share code, notes, and snippets.

@iisaka51
Created October 25, 2022 04:31
Show Gist options
  • Save iisaka51/de492b95fe36df7369534a6a2ec4bf34 to your computer and use it in GitHub Desktop.
Save iisaka51/de492b95fe36df7369534a6a2ec4bf34 to your computer and use it in GitHub Desktop.
namedtuple cannot overwrite __init__()
In [18]: from typing import NamedTuple
...:
...: try:
...: class User(NamedTuple):
...: id: int
...: name: str
...: age: int
...: belongs: str
...:
...: def __init__(self, profile):
...: d = dict(profile)
...: self.id = d['id']
...: self.name = d['name']
...: self.age = d['age']
...: self.belongs = d['belongs']
...:
...: except AttributeError as e:
...: print(e)
...:
Cannot overwrite NamedTuple attribute __init__
In [19]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment