-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rule to recommend str.join takes a list comp over generator #156
Comments
Indeed it can. I think this will require a bit more code to detect than for the existing rules - we'd have to search for a string object, attribute access, and then function call, rather than just a function name. If you want to look into a PR please feel welcome :) |
It would be nice if this checker could detect all functions that accept |
Since flake8 doesn't do any typing, we're restricted to builtins here. |
Actually the linter should enforce |
Yes Tom, thanks! |
It seems it is not much faster these days (the original measurement in SO is 8 years old) |
Still a tiny bit faster - Python 3.8.2:
|
You compared list to tuple, but the issue is about generator (see the description) Try |
|
Python 3.11.2 still shows this perf difference: In [1]: %timeit ",".join([str(i) for i in range(3)])
322 ns ± 1.13 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [2]: %timeit ",".join([str(i) for i in range(300)])
19.2 µs ± 35.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [3]: %timeit ",".join((str(i) for i in range(3)))
407 ns ± 1.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [4]: %timeit ",".join((str(i) for i in range(300)))
22.6 µs ± 60.9 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) I think we could add another rule. |
"".join()
to checker C407
@hmvp @adamchainz Python >=3.8's stdlib ast module actually supports getting the type of functions (although it's not that well documented): https://stackoverflow.com/a/70143560/2444240 Since I assume 3.7 support will be dropped soon, we can look into adding this, (although I would be fine with restricting it to methods from stdlib). |
Yaeh that doesn't help that much - for example, you can't get the type of imported symbols, or the implied types of variables. |
A 10% difference with such tiny numbers is a statistical insignificance. Hardly grounds to make a new rule and nag developers about it. If both approaches take somewhat the same time and memory, then flake8-comprehensions shouldn't be nagging people to prefer one syntax. Or at least make it into an opinionated warning, in the C9xx range. |
The gap has grown significantly in Python 3.12 thanks to Comprehension inlining:
Now about a 40% difference on the small case and 20% on the larger one.
They don't take the same time and memory, that is known. The implementation of |
We're implying here that, in any case, it's better optimizing for faster execution than for less memory occupation. It would be interesting to see differences in memory occupation between these two approaches. |
"".join()
accepts generatorsThe text was updated successfully, but these errors were encountered: