Comment by rand_r

2 days ago

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

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

  • 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...

    • 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.

      2 replies →

    • 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.

      1 reply →

  • 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"

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