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

Python is great, I just wish it had JS-like object destructuring applied to dicts – so convenient!



You mean like

  a = {"foo": "bar", "bar": "foo"}
  b = {**a}
You can do this since 3.5 https://www.python.org/dev/peps/pep-0448/

You can also do dict comprehensions which is much more expressive

  b = {k: v for k,v in a.items() if v == "bar"}
This way you can copy only the parts of a dict you want in a simple one-liner.

https://www.python.org/dev/peps/pep-0274/


Me too. There is a library that solves this: https://github.com/Infinidat/munch

    from munch import Munch

    foo = {'a':1, 'b':2}

    foo = Munch(foo)

    print(foo.a) # 1
    print(foo['a']) # 1


If you can think it, it can be done. There is probably a library for it. It is just not standard.


That I have no doubts about, but having it in the standard lib would be awesome!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: