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...
For further actions, you may consider blocking this person and/or reporting abuse
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.Agreed!
I totally agree but the sad thing is that many of these practices are considered "pythonic".
Nice writing.
For the optional argument check it's also possible to do
so if it's
None
it will assign an empty list or useseq
otherwise.Or are there any downsides with this approach?
E: Ok in case
seq
is already an empty list it will be reassigned anyway.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.
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.
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!
wemake-services / wemake-python-styleguide
The strictest and most opinionated python linter ever!
wemake-python-styleguide
Welcome to the strictest and most opinionated python linter ever.
wemake-python-styleguide
is actually a flake8 plugin with some other plugins as dependencies.Quickstart
You will also need to create a
setup.cfg
file with the configuration.We highly recommend to also use:
Running
This app is still just good old
flake8
And it won't change your existing workflow.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.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.
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!
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.
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?
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.
Very cool tips
Happy you like it :)
After a few paragraphs I've understood a bit more about python. I'm new to programming, thanks for sharing!
Thanks for post!
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...
Also use an auto formatter to write consistent code. I did a comparison here: kevinpeters.net/auto-formatters-fo...
As I understood it's all bad practices..