On Wed, Mar 19, 2014 at 2:42 PM, Antoine Pitrou <solip...@pitrou.net> wrote: > On Tue, 18 Mar 2014 09:52:05 +0200 > Maciej Fijalkowski <fij...@gmail.com> wrote: >> >> We're thinking about doing an optimization where say: >> >> if x in d: >> return d[x] >> >> where d is a dict would result in only one dict lookup (the second one >> being constant folded away). The question is whether it's ok to do it, >> despite the fact that it changes the semantics on how many times >> __eq__ is called on x. > > I don't think it's ok. If the author of the code had wanted only one > lookup, they would have written: > > try: > return d[x] > except KeyError: > pass > > I agree that an __eq__ method with side effects is rather bad, of > course. > What you could do is instruct people that the latter idiom (EAFP) > performs better on PyPy. > > Regards > > Antoine.
I would like to point out that instructing people does not really work. Besides, other examples like this: if d[x] >= 3: d[x] += 1 don't really work. _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com