kurtis_reed 3 days ago

{:} should have been the empty dict, now there's no good solution

  • augusto-moura 3 days ago

    I agree that {:} would be a better empty expression for dicts, but that ship has already sailed. {/} looks like a good enough alternative

    • sitkack 3 days ago

      There is a way to make it work. Python has no problem breaking things across major versions.

      • baq 3 days ago

        Python needed a breaking change for Unicode and a breaking change for exceptions and took it ages ago for a better future today - and it's still remembered as a huge PITA by everyone. I think you'll find everyone in the Python community disagreeing with you about a not-backwards-compatible Python 4.

      • JohnKemeny 3 days ago

        Are you suggesting to bump to Python 4 in order to be able to write `{}` instead of `set()` (or `{/}`) and simultaneously break all existing code using `{}` for dicts?

      • augusto-moura 2 days ago

        Breaking {} to be an empty set would be a HUGE breaking change, a _lot_ of code is already written where it is expected to be an empty dict. I don't think anyone in the Python committee would agree with breaking that

      • IshKebab 2 days ago

        Jesus can you imagine if they announced Python 4? :-D

slightwinder 3 days ago

Making sense, and being good, is not necessary the same.

Yes, having a solution for this makes sense, but the proposed solutions are just not good. Sometimes one has to admit that not everything can be solved gracefully and just stop, hunting the whale.

rand_r 3 days ago

You can use “set()”. Introducing more weird special cases into the language is a bad direction for Python.

  • thayne 3 days ago

    And you can use dict() for an empty dictionary, and list() for an empty list.

    • mrguyorama 2 days ago

      For reasons I don't think I understand, using the functions is "discouraged" because "someone might muck with how those functions work" and the python world, in it's perfect wisdom responded "Oh of course" instead of "That's so damn stupid, don't do that because it would be surprising to people who expect built in functions to do built in logic"

    • mickeyp 3 days ago

      Yes but they are not equivalent. dict and list are factories; {} and [] are reified when the code is touched and then never reinitialised again. This catches out beginners and LLMs alike:

      https://www.inspiredpython.com/article/watch-out-for-mutable...

      • thayne 3 days ago

        That article is about how defaults for arguments are evaluated eagerly. It doesn't real have to do with dict vs {}.

        However, using the literal syntax does seem to be more efficient. So that is an argument for having dedicated syntax for an empty set.

      • spott 2 days ago

        They are equivalent. In function signatures (what your article is talking about), using dict() instead of {} will have the same effect. The only difference is that {} is a literal of an empty dict, and dict is a name bound to the builtin dict class. So you can reassign dict, but not {}, and if you use dict() instead of {}, then you have a name lookup before a call, so {} is a little more efficient.

        • mickeyp 2 days ago

          Right, but it instantiates it _once_ on module load! That is the point I am making; nothing else.

      • fainpul 3 days ago

        Your link doesn't support your argument.

        • mickeyp 2 days ago

          I wrote the link and yes it does. Module evaluations reify {}, [], etc. once. That is why people keep making subtle bugs when they do `def foo(a=[]):` unaware that this will in fact not give you a brand new list on every function call.

          Factory functions like list/tuple/set are function calls and are executed and avoid this problem. Hence why professional python devs default to `None` and check for that and _then_ initialise the list internally in the function body.

          Adding {/} as empty set is great, sure; but that again is just another reified instance and the opposite of set() the function.

      • [removed] 3 days ago
        [deleted]
  • tpm 3 days ago

    No no no, it's a great direction towards becoming the new Perl.