Python progression path – quiz

Python progression path – From apprentice to guru is a popular StackOverflow post. To categorize whether a person should take his beginner/intermediate course, one of the commentors posted this question:

python progression quick

I can better answer this question after reading Fluent Python. Example 1 and Example 2 deal with immutable and mutable data types – respectively. Let’s address each example individually.

Example 1

The id built in method returns the object’s memory address. We’ll use this to inspect the memory address for x and ythroughout the examples.

x and y point to the same memory address when they are first assigned.

Because the value of x is immutable (e.g INT) and cannot be modified, by definition, a new memory address is allocated when x is modified. But, y‘s memory remains unmodified:

Example 2

Like Example 1, the memory address starts off the same:

Now that we are dealing with mutable data types, x can be modified in place and the memory address does not change:

Mutable default arguments

This quiz seems trivial but it isn’t. Understanding this will prevent a novice mistake of using mutable default arguments.