[issue22823] Use set literals instead of creating a set from a list

Raymond Hettinger report at bugs.python.org
Sun Nov 9 04:04:47 CET 2014


New submission from Raymond Hettinger:

There are many places where the old-style of creating a set from a list still persists.  The literal notation is idiomatic, cleaner looking, and faster.

Here's a typical change:


  diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
  --- a/Lib/sre_compile.py
  +++ b/Lib/sre_compile.py
  @@ -22,10 +22,10 @@
   else:
       MAXCODE = 0xFFFFFFFF
 
  -_LITERAL_CODES = set([LITERAL, NOT_LITERAL])
  -_REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT])
  -_SUCCESS_CODES = set([SUCCESS, FAILURE])
  -_ASSERT_CODES = set([ASSERT, ASSERT_NOT])
  +_LITERAL_CODES = {LITERAL, NOT_LITERAL}
  +_REPEATING_CODES = {REPEAT, MIN_REPEAT, MAX_REPEAT}
  +_SUCCESS_CODES = {SUCCESS, FAILURE}
  +_ASSERT_CODES = {ASSERT, ASSERT_NOT}

Here are typical timings:

  $ py34 -m timeit '{10, 20, 30}'
   10000000 loops, best of 3: 0.145 usec per loop

  $ py34 -m timeit 'set([10, 20, 30])'
  1000000 loops, best of 3: 0.477 usec per loop

----------
components: Library (Lib)
keywords: easy
messages: 230879
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Use set literals instead of creating a set from a list
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22823>
_______________________________________


More information about the Python-bugs-list mailing list