Late-binding of function defaults (was Re: What is a function parameter =[] for?)

Marko Rauhamaa marko at pacujo.net
Thu Nov 26 05:54:05 EST 2015


Chris Angelico <rosuav at gmail.com>:

> On Thu, Nov 26, 2015 at 7:27 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> I have wanted to use lists as keys, and there should be no reason to
>> allow mutable tuples. It should be enough to say that the behavior of
>> a dictionary is undefined if a key should mutate on the fly.
>
> Python defines dict-key-validity as being synonymous with hashability.
> It's up to the class author to make sure the object's hash is useful.

   >>> hash([])
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: unhashable type: 'list'

Annoying.

> Behaviour being undefined works just fine [1] in C. It's not so
> popular in Python. I don't think we want to have myriad new and
> experienced programmers trying to figure out why square brackets in
> dict keys are usually fine but occasionally not.

But there's no way to enforce that with any of the myriad new classes.

   Ye blind guides, which strain at a gnat, and swallow a camel.

But I say unto you, Swallow the gnat as well.


Marko



More information about the Python-list mailing list