[Tutor] Unnecessary comprehension warning
DL Neil
PyTutor at danceswithmice.info
Sat Jun 6 22:29:24 EDT 2020
On 7/06/20 2:19 PM, boB Stepp wrote:
> Continuing "Exercise 3.12: Using your library module" in "3.4 Modules" at
> https://dabeaz-course.github.io/practical-python/Notes/03_Program_organization/04_Modules.html.
>
>
> I rewrote another function in report.py, read_prices(), to make use of the
> parse_csv() function in the fileparse module. The rewritten function is:
>
> def read_prices(filename: str) -> Dict[str, float]:
> """Read a file of stock prices and load them into a dictionary."""
> prices = fileparse.parse_csv(filename, types=[str, float],
> has_headers=False)
> stock_prices = {stock_name: stock_price for stock_name, stock_price
> in prices}
> return stock_prices
>
> pylint complains with:
>
> report.py|25 col 1 warning| unnecessary-comprehension: Unnecessary use
> of a comprehension
>
> The dictionary comprehension I'm now using replaces a lengthier for loop
> with indexing instead of nice names. Am I truly doing things in a bad
> Python style as the linter is complaining? Should I be doing this
> differently?
The name "prices" is defined and then used, but enjoys no further reference.
What happens (from PyLint's PoV) if you make the two lines into one?
(overly-complicated and ugly though it would be)
--
Regards =dn
More information about the Tutor
mailing list