Graffiti
Graffiti
Recycle Bin
Recycle Bin
xroi.cc
Welcome to
xroi.cc
Note Graph
Loading...
Loading Note Graph...
Weird python code

Weird python code

Look at the following piece of python code:

1def surprise(my_list = []):
2    print(my_list)
3    my_list.append('x')
4surprise() 
5surprise()

What does it print? On first glance, and if you are unfamiliar with the python concept, you might think it would be:

1[]
2[]

However, the actual output is:

1[]
2[x]

Why? The key point here is that the default argument [] is evaluated only once, when the function is defined, not each time the function is called. This means that the one same list object is used for all calls to surprise() where no argument is provided.

How come this behaviour is desired? well for the most part it doesn't matter - when the type of the default argument is immutable. For example, look at the following code:

1def surprise_int(my_int = 123):
2    print(my_int)
3    my_int += 3
4surprise_int() 
5surprise_int()

This code will print:

1123
2123

Why? because integers are immutable, therefore the line my_int += 3 isn't adding 3 to the reference of the default argument, instead it's creating an entirely new reference, leaving the default argument unchanged, leaving it the way it was evaluated.

Which types are immutable in python? Some examples are Integers, Floating Points, Strings, Tuples, Booleans and Frozensets. This list is finite (However new immutable types can be defined through named tuples or frozen dataclasses).

Globe
Loading...
Loading Globe...
It looks like you're reading this page! Need help?
Dinosaur
00:00 PM