DEV Community

Discussion on: The Proficient Pythonista: List Comprehensions

Collapse
 
sobolevn profile image
Nikita Sobolev

Great article! Thanks a lot!

It is also important to lint your python code generally (and list comprehensions in particular). Because they can contain different problems:

  1. x = (yield a for a in [1, 2, 3]) will be not what you might think it is. Linter can catch this!
  2. x = [lambda y: a + y for a in [1, 2, 3]] will return 4 for x[0](1). And good linter will also catch this error
  3. Good linter will enforce consistency in for a in [1, 2, 3], for a in {1, 2, 3} and for a in (1, 2, 3)
  4. Good linter will catch that for loop part has correct unpacking targets
  5. Great linter will warn you when you comprehensions are too complex
  6. Great linter will tell you that you comprehension is incorrect: like list([x for x in [1, 2, 3]])

I recommend to use wemake-python-styleguide. Check it out:

GitHub logo wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!

wemake-python-styleguide

wemake.services Supporters Build Status Coverage Status Python Version wemake-python-styleguide


Welcome to the strictest and most opinionated python linter ever.

wemake-python-styleguide logo

wemake-python-styleguide is actually a flake8 plugin with some other plugins as dependencies.

Quickstart

pip install wemake-python-styleguide

You will also need to create a setup.cfg file with the configuration.

We highly recommend to also use:

  • flakehell for easy integration into a legacy codebase
  • nitpick for sharing and validating configuration across multiple projects

Running

flake8 your_module.py

This app is still just good old flake8 And it won't change your existing workflow.

invocation resuts

See "Usage" section in the docs for examples and integrations.

We also support Github Actions as first class-citizens Try it out!

What we are about

The ultimate goal of this project is to make all people write exactly the same python code.

flake8 pylint black mypy wemake-python-styleguide
Formats code?
Finds style issues? 🤔 🤔
Finds bugs? 🤔
Collapse
 
mburszley profile image
Maximilian Burszley

It's not about consistency, it's about use-case. A list, tuple and set all solve different problems. Comprehensions should always be simple, otherwise you should be using a different construct that is readable.

Yes, you should use a linter, but almost any editor nowadays will catch the errors you point out and are not necessarily linting issues.