You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was recently working on some bits of codes where most of my data had to be "readonly" (so I'm using immutable types like frozen dataclasses, frozensets, tuples, etc.) but also using plenty of comprehensions. Which made me wonder, since there's no "tuple comprehension" in Python, how I should be writing this code. I did a bit of performance testing, and here's the results:
3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
test_list_comprehension 6.4194597
test_tuple_from_list_comprehension 6.9672235
test_tuple_from_generator_comprehension 8.996260200000002
test_unpack_generator_comprehension 11.207814599999999
3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
test_list_comprehension 5.656617900000128
test_tuple_from_list_comprehension 6.026029500000277
test_tuple_from_generator_comprehension 9.207803900000272
test_unpack_generator_comprehension 10.375420500000018
Unsurprisingly, the difference is even greater in 3.12 with inline list comprehension.
Because of the tuple, the generator is immediately iterated, so you get no benefit from its "lazyness". This is probably true for other stdlib collections that don't have a comprehension syntax, tuple is just the only one I can think of atm.
For this reason, I'm asking for a performance rule with an autofix that transforms code like this:
tuple(aforainb)
into
tuple([aforainb])
Which, unless I'm missing something, is free performance whilst staying readable and pythonic.
It seems this would fit well in the flake8-comprehensions or refurb family of rules.
The text was updated successfully, but these errors were encountered:
Avasam
changed the title
New Rule: Prefer list comprehension over generators to create tuples
New Rule: Prefer list comprehension over generator comprehensions to create tuples
Jun 11, 2024
I was recently working on some bits of codes where most of my data had to be "readonly" (so I'm using immutable types like frozen dataclasses, frozensets, tuples, etc.) but also using plenty of comprehensions. Which made me wonder, since there's no "tuple comprehension" in Python, how I should be writing this code. I did a bit of performance testing, and here's the results:
Unsurprisingly, the difference is even greater in 3.12 with inline list comprehension.
Because of the
tuple
, the generator is immediately iterated, so you get no benefit from its "lazyness". This is probably true for other stdlib collections that don't have a comprehension syntax, tuple is just the only one I can think of atm.For this reason, I'm asking for a performance rule with an autofix that transforms code like this:
into
Which, unless I'm missing something, is free performance whilst staying readable and pythonic.
It seems this would fit well in the
flake8-comprehensions
orrefurb
family of rules.The text was updated successfully, but these errors were encountered: