Hacker News new | past | comments | ask | show | jobs | submit

Lesser known parts of Python standard library

https://www.trickster.dev/post/lesser-known-parts-of-python-standard-library/
loading story #41453048
loading story #41456041
loading story #41453011
loading story #41454189
loading story #41454249
loading story #41456558
loading story #41456805
MappingProxyType is another handy one. It wraps a regular dict/Mapping to create a read-only live view of the underlying dict. You can use it to expose a dict that can't be modified, but doesn't need copying.

https://docs.python.org/3/library/types.html#types.MappingPr...

loading story #41456995
loading story #41455714
loading story #41454264
loading story #41454987
loading story #41455050
loading story #41457276
loading story #41455130
loading story #41456638
loading story #41458193
loading story #41457121
loading story #41457470
loading story #41469721
loading story #41458975
loading story #41456661
loading story #41458558
loading story #41453669
loading story #41461539
loading story #41456593
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
Pydantic, attrs, or dataclasses (standard library option). Or you can override __getattr__ on a dict subclass.
You 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
Use Lua?
loading story #41470224
loading story #41455830