---
title: "Discussion on \"Python Gotchas: 5 Worst Mistakes You Should Avoid\" | Hashnode"
description: "Discussion on \"Python Gotchas: 5 Worst Mistakes You Should Avoid\". Learn to identify traps such as default arguments, list and string concatenation gotchas"
url: "https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Python Gotchas: 5 Worst Mistakes You Should Avoid](https://miguendes.me/python-gotchas?utm_source=hashnode&utm_medium=feed)

Learn to identify traps such as default arguments, list and string concatenation gotchas

Oct 24, 2020 · 9 min read · miguendes.me  
Updated Oct 1, 2021  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#tutorial](https://hashnode.com/tag/tutorial)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Miguel Brito](https://hashnode.com/@miguendes)

@miguendes · London, UK

  
[View profile](https://hashnode.com/@miguendes)

Machine Learning Software Engineer - Hobbyst Technical Writer - Interested in Software Testing, Best Practices, Scalability, and Python.

AI Software Engineer based in London, UK with 6+ years of professional experience developing and releasing software in different programming languages - Hobbyist Technical Writer - Interested in Software Testing, Best Practices, Scalability, Machine Learning/AI, and Python.

## Responses 7

Markdown supported · ⌘↵ to post

[David Bankson](https://hashnode.com/@QuothTheRaven42)

[Dec 4, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5fcaabfe2e4db7780e482885)

The walrus operator is only making a 3-tuple because you are putting into a tuple in the first place. Do a print statement without the extra parens. Then try type-checking the print and it will throw an error...unless you put the parens around it so it sees a tuple. The walrus operator basically starts a new operation the way I've understood it; b := 16 evaluates and prints on its own, so everything is forced into evaluating like that.

[Sobolev Nikita](https://hashnode.com/@sobolevn)

[Oct 29, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f9b3ddf9dacc2579c58d20c)

Thanks a lot! Great points.

Looks like a good linter like [github.com/wemake-services/wemake-python-stylegui…](https://github.com/wemake-services/wemake-python-styleguide) will catch almost all of these problems.

[Alex Hall](https://hashnode.com/@alexmojaki)

[Oct 27, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f98974b91c85e2865f5fb86)

> Since 0 is considered a "Falsy" value, then the comparison returns 'True\`.

No, that's the kind of thing JavaScript does. Most Falsy values (None, empty string/list/dict/etc) are not equal to False.

`False == 0` and `True == 1` because bool is a subclass of int. You can treat booleans just like those numbers, for example `True + True == 2`. This can be very useful.

[Miguel Brito](https://hashnode.com/@miguendes)

[Oct 27, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f98974b91c85e2865f5fb86#reply-5f989ac46ea7602886a170a6)

Interesting, thanks for the correction. I'll update the post.

Regardless, I wish it boolean wasn't subclass of ints.

IMHO, `True + True + True` shouldn't be equal to 3, or `False == 0`. This would avoid bugs such as the ones when using chained comparisons.

[Alex Hall](https://hashnode.com/@alexmojaki)

[Oct 27, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f98974b91c85e2865f5fb86#reply-5f989ea76ea7602886a170d5)

To me it seems obvious that the chained comparisons wouldn't work. I've never seen someone make that mistake before. Doing that breaks extremely easily without the fact about booleans. If `a == b == c` is True, then `(a == b) == c` is guaranteed to be False unless they are all equal to True (or 1).

I often use this property of booleans to write something like `sum(predicate(x) for x in lst)` to count the number of items in a list satisfying a condition, or conveniently making something plural with `f"thing{(len(things) > 1) * 's'}"`. It's hacky but it's fun.

[Edidiong Asikpo (Didi)](https://hashnode.com/@didicodes)

[Oct 26, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f96c39bf9885b0b801807e1)

Very well written [Miguel Brito](https://hashnode.com/@miguendes). Thanks for sharing.

[Kalebu Jordan](https://hashnode.com/@Kalebujordan)

[Oct 24, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f949d3040346172a86c1e4c)

enlightening article, Love it, Thanks for sharing

[Miguel Brito](https://hashnode.com/@miguendes)

[Oct 25, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f949d3040346172a86c1e4c#reply-5f952f7340346172a86c22ea)

Thanks a lot [Jordan Kalebu](https://hashnode.com/@Kalebujordan)

[Nickolai Belakovski](https://hashnode.com/@actinium226)

[Oct 24, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f9487f440346172a86c1dcc)

Great article.

I think the example for the walrus operator could be better. Something like

```
# We want to increment by the number if it's nonzero, otherwise we want to increment by at least 1
x = get_some_number_maybe_0()
if x:
  y += x
else:
  y += 1
```

Which can be transformed with walrus operator to:

```
if x := get_some_number_maybe_0():
    y += x
else:
    y += 1
```

If the example you've shown, `should_run` isn't necessary, you can just base the if statement on the return of the os.environ call.

I guess the above could also be done as y += max(1, get\_some\_number\_maybe\_0()). Dammit, I need to stop finding ways to optimize my examples :P

Edit: The PEP introducing the operator has some better, albeit more complicated examples [python.org/dev/peps/pep-0572](https://www.python.org/dev/peps/pep-0572/#syntax-and-semantics)

[Miguel Brito](https://hashnode.com/@miguendes)

[Oct 25, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f9487f440346172a86c1dcc#reply-5f952f6040346172a86c22df)

Thanks for the feedback [Nickolai Belakovski](https://hashnode.com/@actinium226). I've updated the post to make it clearer.

[Paula Maranhão](https://hashnode.com/@pmaranhaor)

[Oct 24, 2020](https://hashnode.com/posts/python-gotchas/5f93f46afbdce372c9a56d6b/comment/5f94301e40346172a86c19b1)

I was Waaaaat in the whole post. hahahahah Thanks to Python Tutor I can understand the logic. Loved the GIFs, btw!
