Rohan's Block

09/06/2024

Default Args Saga and why use None?

I know default args, but I haven’t dived deep into understanding how they work. I got into this rabbit-hole, while I was debugging some peculiar issue in one of my project.

In Python, default arguments are stored at the time the function is defined, not when it’s called. When you define a function with default arguments, Python evaluates the default argument expressions and binds the resulting objects to the function’s bytecode object.

Here’s how it works:

So, default arguments are captured and stored in the function object’s attributes at the time of function definition. This behavior is a result of Python’s approach to function definition and object binding.