def f(x, l=[]): for i in range(x): l.append(i * i) return l >>> f(2) >>> f(3, [0, 1, 2]) >>> f(3)
f(3)
did not seem to me so unambiguous. Let's see what happens after the initialization of the function f
. To run this code, I will use IPython . >>> f <function __main__.f(x, l=[])> >>> f.__defaults__ ([],)
f.__defaults__
is the variable l
in the function code. >>> f(2) [0, 1]
>>> f <function __main__.f(x, l=[0, 1])> >>> f.__defaults__ ([0, 1],)
l
has the value [0, 1]
due to the variability of the list object in Python and passing the function arguments as a reference. >>> f(3, [0, 1, 2]) [0, 1, 2, 0, 1, 4] >>> f <function __main__.f(x, l=[0, 1])>
list
object as a variable l
. >>> f(3) [0, 1, 0, 1, 4] >>> f <function __main__.f(x, l=[0, 1, 0, 1, 4])>
f(3)
, Python does not use an empty list that is defined in the function code, it uses the variable l
with values from f.__defaults__ ([0, 1])
.'l'
to 'None'
). def f(x, l=None): if l is None: l = [] for i in range(x): l.append(i * i) return l >>> f(2) [0, 1] >>> f(3, [0, 1, 2]) [0, 1, 2, 0, 1, 4] >>> f(3) [0, 1, 4]
Source: https://habr.com/ru/post/459518/
All Articles