MAINT: start applying ruff/Perflint rules (PERF)#26876
MAINT: start applying ruff/Perflint rules (PERF)#26876DimitriPapadopoulos wants to merge 1 commit intonumpy:mainfrom
Conversation
| ret[k] = v | ||
|
|
||
| return ret | ||
| return { |
There was a problem hiding this comment.
I don't think this is an improvement.
There was a problem hiding this comment.
List and dict comprehensions are faster and very commonly used. What exactly don't you like? Having code after return instead of an intermediate variable? If so, it can easily be changed to:
ret = {
k: v
for d in dicts
for k, v in d.items()
}
return ret
|
I think a lot of these obfuscate the code rather than improve it. |
|
I don't disagree. These were semi-manual fixes any way. Probably the most useful changes here are of this type, changing: a = []
for x in sequence:
if cond:
a.append(x)to: a = [
x
for x in sequence
if cond
]List and dict comprehensions are more efficient, and for many programmers not more obfuscate, once you get used to them. |
|
Thinking about it again:
|
FWIW, list comprehensions are a commonly used example in the excellent The Programmer's Brain of a confusing construct. Generally speaking, it can sometimes aid readability in the simpler cases where you can transform a 4-line snippet with lots of repetition into a nice DRY one-liner. In Hermans' terms, this can reduce the diffuseness of the code. But in the more complicated cases, like many of these in the PR, especially those with As a rule, I'm probably going to use a comprehension if it gets me to a one-liner, but probably opt for a |
I am not aware of a neuroscience study showing that our brain needs that order. In German, the past participle is rejected at the end of the sentence and the language is not less clear than English. In any case, the order would be a Python shortcoming. Numpy remains a Python library. |
|
Point taken, and no I don't have any specific reference for you about that particular point. Most eye-tracking studies can't resolve movements that small (but I will note that natural language is not an ideal comparison; we have specialized wetware for our native languages, natural language sentences are generally shorter, and context often overdetermines which way a sentence is going to fall out in any case). There are a number of studies ranking the general readability/understandability of Python list comprehensions lower than the corresponding
|
Will help with #24994.