ecoCode
ecoCode copied to clipboard
[EC75] [Python] Concatenate Strings in loop can be applied to Python as well
(TO BE MEASURED) The Rule EC75 about String concatenation in a loop is said "Not applicable" for Python, but there actually is a way to generate strings that might be a better manner than concatenation in a loop: creating a list of string using a list comprehension then calling the join() built-in.
Non-compliant code example:
s = ""
for fruit in fruits:
s += fruit.name
Compliant code example:
l = [fruit.name for fruit in fruits]
"".join(l)
Hi @LordPatate, thank you for your issue and the new idea ! We aren't Python experts, and your contribution is a chance for us !
If we see the error message of EC75 rule, we can observe that the rule is specific for Java because we talk about StringBuilder. Any way, the idea of this rule, can maybe applied to other languages. If yes, we will must change the error message.
By the other hand, this rule is really good for java because the usage of StringBuilder avoid construction of many String objects in memory, thus environment impact Could you tell us how your idea can improve environment impact, with CPU or memory improvements ?
TO DISCUSS INSIDE CORE-TEAM : opinion about this new idea ?
Hi. I'm thinking that using generator expression are generally more efficient than traditional "for" loops in Python and that Python built-ins like the join
function are often implemented directly in C when using the CPython interpreter, making it more efficient than equivalent Python code.
Those are assumptions that needs to be verified through measurements.
The benefits would likely be seen in term of CPU consumption
@LordPatate, thank you for explanation ... do you have some references documentation or studies about this way of implementation and CPU consumption ? (to reference them in our documentation for future explanations)
I don't have any references sadly. It's just my intuition, which is why I said it has to be measured
We are currently working on this subject (with Societe Generale company) Rules related to this subject should be committed soon.
(seen with @dedece35 and @jhertout )