Overwriting in Python: Tricky. Dangerous. Powerful

PYTHON PROGRAMMING

Although overwriting objects is a typical Python coding technique, it can lead to unexpected effects. You need to know how to use it to utilize its strengths.

27 min read

22 hours ago

Overwriting in Python can be dangerous: Don’t enter without reconsideration. Photo by Raúl Nájera on Unsplash

I’ve overwritten plenty of various objects in Python. If you’ve spent your share of hours coding in Python, you have, too. This is because, at least in Python, overwriting objects lies at the core of the language.

I’ve overwritten variables but also functions, classes, and class methods — even exceptions:

And indeed, we’ll differentiate between overwriting variables and callables. The difference between these two processes is actually quite significant, with the latter being more tricky. And even though our focus is on overwriting callables, as it’s a more advanced technique, we’ll also discuss overwriting variables, as it offers a good starting point for our discussion.