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

Chris Angelico rosuav at gmail.com
Thu Nov 26 02:14:02 EST 2015


On Thu, Nov 26, 2015 at 5:52 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Steven D'Aprano <steve at pearwood.info>:
>
>> Making tuples mutable would break their use as dictionary keys, which is a
>> *critical* use.
>
> No, it wouldn't. Any object that provides __hash__() and __eq__() can be
> used as a key.
>
> Nothing prevents using mutable objects as keys in Python.
>
>

Sure, you _can_. But if the key's hash changes between dict insertion
and retrieval, all manner of invariants will break, and likewise if
two equal objects have different hashes. From which you can deduce
logically that any object used as a key must remain (not) equal to
everything that it was (not) equal to from that time until it is
looked up... which basically means its value mustn't change. It must
be immutable.

ChrisA



More information about the Python-list mailing list