DEV Community

Cover image for Good and Bad Practices of Coding in Python

Good and Bad Practices of Coding in Python

Duomly on October 28, 2019

This article was originally published at: https://www.blog.duomly.com/good-and-bad-practices-of-coding-in-python/ Python is a high-level multi-par...
Collapse
 
idanarye profile image
Idan Arye

Please don't treat numbers as booleans. It makes sense when you are checking for None or for emptiness, but in case of numbers it's just confusing. That's an artifact of the C heritage all languages and programmers have nowadays, but that doesn't mean we should be using it.

Collapse
 
arnauddupuis profile image
Arnaud Dupuis

Agreed!

Collapse
 
tariqabughofa profile image
Tariq Abughofa

I totally agree but the sad thing is that many of these practices are considered "pythonic".

Collapse
 
schneebuzz profile image
schneebuzz • Edited

Nice writing.

For the optional argument check it's also possible to do

seq = seq or []

so if it's None it will assign an empty list or use seq otherwise.
Or are there any downsides with this approach?
E: Ok in case seq is already an empty list it will be reassigned anyway.

Collapse
 
herobank110 profile image
David Kanekanian

Checking an optional argument evaluates to True will not do the same as explicitly checking it is not None. If an empty list was passed as an argument it would still evaluate to False and a new list would be made.

Collapse
 
philipstarkey profile image
Phil Starkey

Was going to say the same thing! That part of the article is definitely poor advice, especially for newcomers who are likely not already be confused by the behaviour.

Collapse
 
sobolevn profile image
Nikita Sobolev

Great guide! It covers so many common problems!

And almost all of these checks can be automated with wemake-python-styleguide: it is a linter for python that can catch common mistakes and bad code, including stylistic and semantic issues.

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 codecov 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 results

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
 
paddy3118 profile image
Paddy3118

The Pythonic way is to exploit the fact that zero is interpreted as False in a Boolean context,

If the test is for conceptually numeric zero then best to test for the number; especially if there are allied tests for different numbers adjacent.

Collapse
 
laurakateholley profile image
LauraHolley

I'm new to Python so appreciated the advice and tips in this article. I also appreciate the comments offering alternatives and opposing a couple of the points! Shows there are different approaches to writing "Pythonic" code.
Thanks!

Collapse
 
arnauddupuis profile image
Arnaud Dupuis

The late binding thing is clearly a bug. I don't understand how the python community consider that a feature.
It look to me that some people thought of optimizing the computing speed of variables allocation and decided it was shorter to reference the same heap space instead of doing a new stack allocation. It is indeed faster but leads to that obviously flawed behavior.
Now I'm not developing the interpreter so I might be totally wrong but so far I haven't seen a valid justification to that behavior.

Collapse
 
idanarye profile image
Idan Arye

What do you mean by "late binding"? AFAIK late binding is a slightly different term (with slightly different semantics) for dynamic typing, but from the way you talk about this it seems you are referring to the default arguments gotcha?

Collapse
 
thebouv profile image
Anthony Bouvier

I want to like this.

But I don't.

Because it is basically an advertisement for a non-sponsor of DEV.to (afaict).

So it feels bad. I don't trust a single heart, unicorn, or whatever on this article.

Collapse
 
omwakwe profile image
Starford Omwakwe • Edited

Very cool tips

Collapse
 
duomly profile image
Duomly

Happy you like it :)

Collapse
 
iioaia profile image
iioaia • Edited

After a few paragraphs I've understood a bit more about python. I'm new to programming, thanks for sharing!

Collapse
 
mayronceccon profile image
Mayron Ceccon

Thanks for post!

Collapse
 
hungluong profile image
Hung Luong

I feel chaining assignment to be a gun pointing at your foot. It looks nice with literals, but given that we almost always assign variables to something, I don't know...

Collapse
 
igeligel profile image
Kevin Peters

Also use an auto formatter to write consistent code. I did a comparison here: kevinpeters.net/auto-formatters-fo...

Collapse
 
rashe profile image
Rafael Shemesh

As I understood it's all bad practices..