Skip to content

Instantly share code, notes, and snippets.

@manatlan
Last active November 9, 2025 16:04
Show Gist options
  • Select an option

  • Save manatlan/24ec7c866311cbe66575b88076e336dc to your computer and use it in GitHub Desktop.

Select an option

Save manatlan/24ec7c866311cbe66575b88076e336dc to your computer and use it in GitHub Desktop.
Convert dict to dict with attribut
class Jdict(dict):
def __new__(cls, obj):
if isinstance(obj, dict):
instance = super().__new__(cls)
instance.update(obj)
return instance
elif isinstance(obj, list):
return [cls(i) for i in obj]
else:
return obj
def __getattr__(self, name: str):
if name in self:
return Jdict(self[name])
else:
return f"?{name}?"
if __name__=="__main__":
x=Jdict(dict(a=12))
print(x) # -> {'a': 12}
print(x.a) # -> 12
print(x.b) # -> '?b?'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment