June 2018
Beginner
508 pages
13h 8m
English
One thing to be very aware of with Python is that default values are created at def time, therefore, subsequent calls to the same function will possibly behave differently according to the mutability of their default values. Let's look at an example:
# arguments.defaults.mutable.pydef func(a=[], b={}): print(a) print(b) print('#' * 12) a.append(len(a)) # this will affect a's default value b[len(a)] = len(a) # and this will affect b's onefunc()func()func()
Both parameters have mutable default values. This means that, if you affect those objects, any modification will stick around in subsequent function calls. See if you can understand the output of those calls:
$ python arguments.defaults.mutable.py[]{}############ ...Read now
Unlock full access