A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/2014-March/133243.html below:

[Python-Dev] Intricacies of calling __eq__

[Python-Dev] Intricacies of calling __eq__ [Python-Dev] Intricacies of calling __eq__Steven D'Aprano steve at pearwood.info
Tue Mar 18 15:21:16 CET 2014
On Tue, Mar 18, 2014 at 01:21:05PM +0200, Maciej Fijalkowski wrote:

> note that this is specifically about dicts, where __eq__ will be
> called undecided number of times anyway (depending on collisions in
> hash/buckets which is implementation specific to start with)

Exactly. Using a __eq__ method with side-effects is a good way to find
out how many collisions your dict has :-)

But specifically with your example,

    if x in d:
        return d[x]

my sense of this is that it falls into the same conceptual area as the 
identity optimization for checking list or set containment: slightly 
unclean, but justified. Provided d is an actual built-in dict, and it 
hasn't been modified between one call and the next, I think it would be 
okay to optimize the second lookup d[x].

A question: how far away will this optimization apply?

    if x in d:
        do_this()
        do_that()
        do_something_else()
        spam = d[x]

Assuming no modifications to d, will the second lookup still be 
optimized?



-- 
Steven
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4