This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "if x in setliteral" peepholer optimization
Type: Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: georg.brandl, gvanrossum, rhettinger
Priority: normal Keywords: patch

Created on 2006-08-28 17:38 by georg.brandl, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
set-peepholer.c georg.brandl, 2006-08-28 17:38 #1
Messages (5)
msg51000 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-08-28 17:38
Like "for x in tuple_or_list", this patch rewrites "for
x in set" to use a constant frozenset.
msg51001 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2006-08-29 18:24
Logged In: YES 
user_id=6380

Um, "for x in [1,2,3]" doesn't get any treatment. What would
be the point of writing "for x in {1,2,3}" anyway?

I'd rather reject this as premature optimization.
msg51002 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-08-29 19:08
Logged In: YES 
user_id=849994

Argh. Like in the py3k mail, I confusedly interchanged "for
x in set" and "if x in set".

if x in [1,2,3] does get special treatment, and optimizing
the "in" test does make sense.
msg51003 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-08-29 21:26
Logged In: YES 
user_id=80475

This implementation too simplistic, you need to use a
subclass of frozenset that overrides the __contains__()
method to return False when the argument is not hashable. 
Otherwise, you end-up with a semantic change for:

   x = {}
   if x in [1,2,3]:
       print 'Not Found'
msg51004 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2006-08-29 21:57
Logged In: YES 
user_id=6380

Let me reject this as a waste of time right now.

See my post "Premature optimization and all that" in the
py3k list.
History
Date User Action Args
2022-04-11 14:56:19adminsetgithub: 43903
2008-01-06 22:29:46adminsetkeywords: - py3k
versions: + Python 3.0
2006-08-28 17:38:19georg.brandlcreate