[Python-ideas] several different needs [Explicit variable capture list]

Chris Angelico rosuav at gmail.com
Wed Jan 27 17:15:58 EST 2016


On Thu, Jan 28, 2016 at 4:27 AM, Jim J. Jewett <jimjjewett at gmail.com> wrote:
>> Here I disagree completely. Why do we have tuple,
>> or frozenset? Why do dicts only take immutable keys?
>> Why does the language make it easier to build
>> mapped/filtered copies in place? Why can immutable
>> objects be shared between threads or processes trivially,
>> while mutable objects need locks for threads and heavy
>> "manager" objects for processes? Mutability is a very big deal.
>
> Those are all "if you're living with these restrictions anyhow,
> and you tell the compiler, the program can run faster."
>
> None of those sound important in terms of "What does this program
> (eventually) do?"

The nature of hash tables and equality is such that if an object's
value (defined by __eq__) changes between when it's used as a key and
when it's looked up, bad stuff happens. It's not just an optimization
- it's a way for the dict subsystem to protect us against craziness.
Yes, you can bypass that protection:

class HashableList(list):
    def __hash__(self): return hash(tuple(self))

but it's a great safety net. You won't unexpectedly get KeyError when
you iterate over a dictionary - you'll instead get TypeError when you
try to assign. Is that a semantic question or a performance one?

ChrisA


More information about the Python-ideas mailing list