DEV Community

John Mark Bulabos
John Mark Bulabos

Posted on

5 Common Python Mistakes that Even Experts Make

Hey there, fellow code wrangler! Ready to embark on a side-splittingly amusing adventure through the land of Python mistakes? Excellent, because it’s about time we shed light on those sneaky little bugs that even top-tier Pythonistas often trip over. Sure, we might be programming in Python, but sometimes we all feel like we're trying to wrangle an actual python—slippery and slightly hard to predict.

1. A classic case of Mutable Default Arguments

Just like those who believe pineapple belongs on pizza, some developers think it's fine to use mutable default arguments in function definitions. Here's the deal: Python’s default arguments are evaluated only once, when the function is defined. So, using mutable defaults, like a list or a dictionary, leads to some interestingly unexpected results.

def add_fruit(fruits=[]):
    return fruits
Enter fullscreen mode Exit fullscreen mode

You'd expect to see a single "apple" each time you call add_fruit(), right? Wrong! It's like saying you only ate one cookie, but the cookie jar is suspiciously empty. Those apples keep piling up every time you call the function without an argument!

2. Misunderstanding Python Scope

One does not simply walk into the realm of Python scope without occasionally getting lost. Variables defined in a function are confined to that function, almost like a pet python in its vivarium.

x = 10
def change_x():
    x = 20
Enter fullscreen mode Exit fullscreen mode

Guess what? x is still 10. It's like telling your python (the reptile) to tidy its vivarium while you're out; you'll come back to find it exactly as messy as you left it. If you want to change x, you need to declare it as global within the function.

3. Copy? What Copy?

Ah, the ole mutable copy trap, where list2 = list1 doesn’t mean what you think it does. If you thought you'd just cloned a list, think again.

list1 = [1, 2, 3]
list2 = list1
Enter fullscreen mode Exit fullscreen mode

Surprise! Both lists now end with a 4, as if your python managed to sneak into both boxes you put out for it to choose from. To actually make a copy, use list1.copy(), list2 = list1[:], or list(list1). Now, no more unexpected twinning.

4. The Name Shadowing

Reusing variable names can lead to some hair-pulling-out moments. It's like naming all your pet pythons "Monty"; you're just asking for trouble.

print = 5
print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

This will raise a TypeError, and your print() function is now out of order, just like your python who figured out how to open the fridge and ate all the eggs. To get print() back, you'll need to delete the variable with del print.

5. Misusing == and is

Using == and is interchangeably is like petting your python and expecting it to purr—it just doesn’t work that way. == checks if values are equal, while is checks if two variables point to the same object.

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False
Enter fullscreen mode Exit fullscreen mode


a and b` contain the same values but aren't the same list, just like how two identical-looking pythons are not the same python (unless you've discovered cloning, in which case, we need to talk).

There you have it, five common Python mistakes that can slither up on even the most seasoned developers. But remember, even if Python is about as predictable as a live python's behavior, it's also just as much fun to engage with—once you get to know it. Happy coding!

Top comments (0)