13 Python Quirks That Will Surprise You

ArjanCodes////2 min read

The Hidden Mechanics of Python Objects

Python often feels like magic until it doesn't. You write code that seems perfectly logical, only to have the interpreter throw a curveball that leaves you questioning your sanity. These aren't just bugs; they are the result of deep-seated design decisions in that prioritize performance or historical consistency over immediate intuition. Understanding these quirks is the difference between a developer who merely writes code and one who truly understands the runtime. Let's peel back the curtain on some of the most surprising behaviors you'll encounter.

13 Python Quirks That Will Surprise You
13 Python Quirks That Will Surprise You

Memory Optimization and the Integer Cache

One of the most jarring realizations for new developers is that the identity operator (is) doesn't always behave like the equality operator (==). This stems from a performance optimization known as integer caching. To save memory, pre-allocates small integers—typically between -5 and 256. When you create a variable with the value 10, simply points that variable to the pre-existing object in memory.

However, move outside this range, and the behavior changes. If you define two variables as 257, creates two distinct objects. An identity check will return False. This gets even more complex because the interpreter might optimize literals in the same code block, caching even larger numbers. Relying on is for value comparison is a dangerous game; always stick to == unless you are specifically checking if two variables point to the exact same memory address.

The Trap of Default Mutable Arguments

We have all done it: defined a function with a default argument like def add_item(item, items=[]). It looks clean, but it hides a massive pitfall. In , default arguments are evaluated only once at the time of function definition, not every time the function is called.

This means that the empty list [] is created once and persists across every single call to that function. If you append an item to it, that item stays there for the next caller. This shared state can lead to

Topic DensityMention share of the most discussed topics · 12 mentions across 7 distinct topics
50%· programming languages
8%· people
8%· programming languages
8%· software
8%· programming languages
Other topics
17%
End of Article
Source video
13 Python Quirks That Will Surprise You

13 Python Quirks That Will Surprise You

Watch

ArjanCodes // 26:25

On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!

What they talk about
AI and Agentic Coding News
Who and what they mention most
Python
33.3%5
Python
20.0%3
Python
20.0%3
Pydantic
13.3%2
2 min read0%
2 min read