Skip to content

Use dict and set literals instead of calls to dict() and set() - #5559

Merged
carltongibson merged 1 commit into
encode:masterfrom
jdufresne:literals
Nov 6, 2017
Merged

Use dict and set literals instead of calls to dict() and set()#5559
carltongibson merged 1 commit into
encode:masterfrom
jdufresne:literals

Conversation

@jdufresne

Copy link
Copy Markdown
Contributor

Set literals are available on all supported Python versions. They are idiomatic and always faster:

$ python3 -m timeit '{}'
10000000 loops, best of 3: 0.0357 usec per loop
$ python3 -m timeit 'dict()'
10000000 loops, best of 3: 0.104 usec per loop
$ python3 -m timeit '{1, 2, 3}'
10000000 loops, best of 3: 0.0754 usec per loop
$ python3 -m timeit 'set([1, 2, 3])'
1000000 loops, best of 3: 0.228 usec per loop

Set literals are available on all supported Python versions. They are
idiomatic and always faster:

$ python3 -m timeit '{}'
10000000 loops, best of 3: 0.0357 usec per loop
$ python3 -m timeit 'dict()'
10000000 loops, best of 3: 0.104 usec per loop

$ python3 -m timeit '{1, 2, 3}'
10000000 loops, best of 3: 0.0754 usec per loop
$ python3 -m timeit 'set([1, 2, 3])'
1000000 loops, best of 3: 0.228 usec per loop
@xordoquy

xordoquy commented Nov 6, 2017

Copy link
Copy Markdown
Contributor

How much does it impacts a full request ?

@carltongibson carltongibson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. This looks good.

I think the speed issue is a non-issue really. For me it's more that this syntax will be idiomatic as time passes.

@carltongibson carltongibson added this to the 3.7.2 milestone Nov 6, 2017
@carltongibson
carltongibson merged commit 0552810 into encode:master Nov 6, 2017
@jdufresne

Copy link
Copy Markdown
Contributor Author

How much does it impacts a full request ?

Probably not much in practice. However, literals are always supported, always idiomatic, always readable, and always faster. Most of the time, they're always the preferred choice, even without benchmarks.

@jdufresne
jdufresne deleted the literals branch November 9, 2017 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants