On Mon, Jan 17, 2022 at 7:44 PM Paul Moore <p.f.mo...@gmail.com> wrote: > > On Mon, 17 Jan 2022 at 10:12, Steven D'Aprano <st...@pearwood.info> wrote: > > That would make the creation of frozensets more efficient, possibly > > encourage people who currently are writing slow and inefficient code > > like > > > > targets = (3, 5, 7, 11, 12, 18, 27, 28, 30, 35, 57, 88) > > if n in targets: > > do_something() > > > > to use a frozenset, as they probably should already be doing. > > More realistically, would they not use a set already, as in > > targets = {3, 5, 7, 11, 12, 18, 27, 28, 30, 35, 57, 88} > if n in targets: > do_something() > > ? >
This is very inefficient because building a set is much heavier in `n in tuple`. We should write `if n in {3, 5, 7, 11, 12, 18, 27, 28, 30, 35, 57, 88}` for now. Or we should write `_TARGETS = frozenset((3, 5, 7, 11, 12, 18, 27, 28, 30, 35, 57, 88))` in global scope and use it as `if n in _TARGETS`. -- Inada Naoki <songofaca...@gmail.com> _______________________________________________ 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/RAW2KG77YI5GS4AXLLFKLBR3PTVE65OA/ Code of Conduct: http://python.org/psf/codeofconduct/