On Fri, 8 May 2020 17:40:31 -0700
Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:

> So, the OP is right that (1,2,3)==[1,2,3] would sometimes be handy,
> the opponents are right that it would often be misleading, and the
> question isn’t which one is right ...

That's a good summary.  Thank you.  :-)

> [1] If anyone still wants to argue that using a tuple as a hashable
> sequence instead of an anonymous struct is wrong, how would you change
> this excerpt of code:
> 
>     memomean = memoize(mean, key=tuple)
>     def player_stats(player):
>         # …
>         … = memomean(player.scores) …
>         # …
> 
> Player.scores is a list of ints, and a new one is appended after each
> match, so a list is clearly the right thing. But you can’t use a list
> as a cache key. You need a hashable sequence of the same values. And
> the way to spell that in Python is tuple.

Very clever.  Then again, it wouldn't be python-ideas if it were that
simple!  "hashable sequence of the same values" is too strict.  I think
all memoize needs is a key function such that if x != y, then key(x) !=
key(y).

    def key(scores):
        ','.join(str(-score * 42) for score in scores)

    memomean = memoize(mean, key=key)
    def player_stats(player):
        # …
        … = memomean(player.scores) …
        # …

Oh, wait, even that's too strict.  All memoize really needs is if
mean(x) != mean(y), then key(x) != key(y):

    memomean = memoize(mean, key=mean)
    def player_stats(player):
        # …
        … = memomean(player.scores) …
        # …

But we won't go there.  ;-)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BGHFJV2LKC7RYMZS6ZJTM26X4ADNXJNR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to