4 ms·
what do people use when they want shorthand for something like this: a['foo'] = 20 a['bar'] = 9 where you want to be able to do: a.foo = 20 a.bar = 9
by m463 2y ago
what do people use when they want shorthand for something like this:
a['foo'] = 20
a['bar'] = 9
where you want to be able to do:
a.foo = 20
a.bar = 9
- hooverd 2y agoPydantic, attrs, or dataclasses (standard library option). Or you can override __getattr__ on a dict subclass.
- Am4TIfIsER0ppos 2y agoUse Lua?
- Sohcahtoa82 2y agoOr JavaScript.
- Too 2y agoYou don't. Use dataclasses instead to get type checking. Otherwise there are tons of tricks to get what you want. To add to the list posted in sibling: a = vars(a) # readonly print(a.foo) or class Obj: pass; a = Obj() a.foo = 20