code-readability

How to deal with Pylint's "too-many-instance-attributes" message?

How to deal with Pylint's "too-many-instance-attributes" message? Question: I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that the linter should not have the last …

Total answers: 4

Python list comprehension – want to avoid repeated evaluation

Python list comprehension – want to avoid repeated evaluation Question: I have a list comprehension which approximates to: [f(x) for x in l if f(x)] Where l is a list and f(x) is an expensive function which returns a list. I want to avoid evaluating f(x) twice for every non-empty occurance of f(x). Is there …

Total answers: 12

Scipy Sparse Row/Column Dot Products

Scipy Sparse Row/Column Dot Products Question: What is the readable and efficient way to compute the dot product between two columns or rows of a sparse matrix using scipy? Let’s say that we want to take the dot product of two vectors x and y, two columns of sparse matrix A, then I’m currently doing: …

Total answers: 2

Code-style for indention of multi-line 'if' statement?

Code-style for indention of multi-line 'if' statement? Question: When indenting long if conditions, you usually do something like this (actually, PyDev indents like that): if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely() However, this puts the block started by the if statement on the same indentation level as the last part of the …

Total answers: 9

`if key in dict` vs. `try/except` – which is more readable idiom?

`if key in dict` vs. `try/except` – which is more readable idiom? Question: I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do …

Total answers: 11