Most active commenters

    80 points _ZeD_ | 26 comments | | HN request time: 1.123s | source | bottom
    1. zdimension ◴[] No.44537739[source]
    Did not know that such things could be accomplished by registering a new file coding format. Reminds me of https://pypi.org/project/goto-statement/
    replies(2): >>44538009 #>>44538014 #
    2. nine_k ◴[] No.44537987[source]
    In short, it runs a text preprocessor as the source text decoder (like you would decode from Latin-1 or Shift-JIS to Unicode).
    replies(1): >>44539341 #
    3. zahlman ◴[] No.44538009[source]
    This one is arguably even more of a hack; it's working at the source code level rather than the AST level.

    The "coding" here is a bytes-to-text encoding. The Python lexer expects to see character data; you get to insert arbitrary code to convert the bytes to characters (or just use existing schemes the implement standards like UTF-8).

    4. ◴[] No.44538014[source]
    5. zelphirkalt ◴[] No.44538027[source]
    I found dictionary unpacking to be quite useful, when you don't want to mutate things. Code like:

        new_dict = {**old_dict, **update_keys_and_values_dict}
    
    Or even complexer:

        new_dict = {
            **old_dict,
            **{
                key: val
                for key, val in update_keys_and_values_dict
                if key not in some_other_dict
            }
        }
    
    It is quite flexible.
    replies(1): >>44538627 #
    6. sco1 ◴[] No.44538181[source]
    The author also has an accompanying video: https://youtu.be/eqiM0xRmFJg
    7. andy99 ◴[] No.44538191[source]

      def u(**kwargs):
        return tuple(kwargs.values())
    
    Am I missing something, is this effectively the same?

    *I realize the tuple can be omitted here

    replies(4): >>44538290 #>>44538355 #>>44539536 #>>44539641 #
    8. Grikbdl ◴[] No.44538290[source]
    Yours relies on ordering, OP's presumably does not.
    9. Izkata ◴[] No.44538355[source]
    You have to pull them out by key name, and not just get everything. Here's a working version, though with a totally different syntax (to avoid having to list the keys twice, once as keys and once as resulting variable names):

      >>> def u(locals, dct, keys):
      ...     for k in keys:
      ...         locals[k] = dct[k]
      ... 
      >>> dct = {'greeting': 'hello', 'thing': 'world', 'farewell': 'bye'}
      >>> u(locals(), dct, ['greeting', 'thing'])
      >>> greeting
      'hello'
      >>> thing
      'world'
      >>> farewell
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      NameError: name 'farewell' is not defined
    
    
    Modifying locals() is generally frowned upon, as there's no guarantee it'll work. But it does for this example.
    10. peter422 ◴[] No.44538627[source]
    I love the union syntax in 3.9+:

      new_dict = old_dict | update_keys_and_values_dict
    replies(1): >>44538710 #
    11. parpfish ◴[] No.44538710{3}[source]
    Don’t forget the in place variant!

      the_dict |= update_keys_and_values_dict
    replies(1): >>44539624 #
    12. kristjansson ◴[] No.44539069[source]
    While not nearly as fun as the OP, I’d note that this sort of unpacking is very pleasant in the newish PEP636 match case statements:

    https://peps.python.org/pep-0636/#matching-builtin-classes

    13. nikisweeting ◴[] No.44539220[source]
    I would donate $500 to the PSF tomorrow if they added this, the lack of it is daily pain
    replies(2): >>44539245 #>>44540127 #
    14. almostgotcaught ◴[] No.44539245[source]
    you can't do this consistently across all cases without compiler assistance (see https://doc.rust-lang.org/book/ch19-03-pattern-syntax.html or https://peps.python.org/pep-0636/#matching-builtin-classes linked below).
    replies(1): >>44539403 #
    15. agumonkey ◴[] No.44539341[source]
    yeah that's the funny part here, would never have thought of this
    16. agumonkey ◴[] No.44539343[source]
    Coming from lisp/haskell I always wanted destructuring but after using it quite a lot in ES6/Typescript, I found it's not always as ergonomic and readable as I thought.
    17. nikisweeting ◴[] No.44539403{3}[source]
    perfect is enemy of good imo, dict destructuring is so valuable that I'm willing to bend some rules / add some rules to make it possible. can't we just copy whatever JS does?
    replies(2): >>44539489 #>>44539635 #
    18. skeledrew ◴[] No.44539489{4}[source]
    If it's that valuable to you personally you can use that project to remove your "daily pain". No need to inflict the pain caused by such a thing being present in official Python. Some of us like for the language to remain highly readable.
    19. sischoel ◴[] No.44539536[source]
    Or use itemgetter:

      >>> from operator import itemgetter
      >>> dct = {'greeting': 'hello', 'thing': 'world', 'farewell': 'bye'}
      >>> thing, greeting = itemgetter("thing", "greeting")(dct)
      >>> thing
      'world'
      >>> greeting
      'hello'
    replies(1): >>44539632 #
    20. masklinn ◴[] No.44539624{4}[source]
    No no, do forget about it: like += for lists, |= mutates “the dict”, which often makes for awkward bugs.

    And like += over list.extend, |= over dict.update is very little gain, and restricts legal locations (augmented assignments are statements, method calls are expressions even if they return "nothing")

    21. ◴[] No.44539632{3}[source]
    22. almostgotcaught ◴[] No.44539635{4}[source]
    > perfect is enemy of good imo

    You can't land a language feature that only sometimes works - that's absolutely horrid UX.

    > can't we just copy whatever JS does?

    I wasn't aware that js does this and I don't know it's implemented. So maybe I should retract my claim about compiler assistance.

    23. masklinn ◴[] No.44539641[source]
    TFA looks things up by key, and allows pulling a subset of the dict.
    24. qwertox ◴[] No.44539829[source]
    This confuses me a bit

      dct = {'a': [1, 2, 3]}
      {'a': [1, *rest]} = dct
      print(rest)  # [2, 3]
    
    Does this mean that i can use?

      dct = {'a': [1, 2, 3]}
      {'b': [4, *rest]} = dct
      print(rest)  # [2, 3]
    
    and more explicit

      dct = {'a': [1, 2, 3]}
      {'_': [_, *rest]} = dct
      print(rest)  # [2, 3]
    25. IshKebab ◴[] No.44540127[source]
    You shouldn't be using dicts for data that you know the name of anyway - use dataclasses or named tuples. Dicts are best for things with keys that are not known at compile time.