Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Default Arguments Evaluation [duplicate]

I was reading the python tutorial from Python Documentation Release 2.7.10 and I came across something like this.

Code

def fun1(a,L=[]):
    L.append(a)
    return L

print fun1(1)
print fun1(2)
print fun1(3)

def fun2(a,L = None):
    if L is None:
        L=[]
    L.append(a)
    return L

print fun2(1)
print fun2(2)
print fun2(3)

Output

[1]
[1, 2]
[1, 2, 3]
[1]
[2]
[3]

Process finished with exit code 0

If the L=[] in the first function fun1() is getting called only once , the output of fun1()is fine. But then why L=None is getting called every time in fun2().

like image 493
Nitin Pandey Avatar asked Oct 25 '25 08:10

Nitin Pandey


1 Answers

L=[] in the function declaration makes Python essentially do this:

  • this function has a parameter named L
  • its default argument is [], let's set this particular [] aside and use it anytime there's no parameter passed for L
  • every time the function is called, create a variable L, and assign it either the passed parameter or the value we set aside earlier

So, the [] part is getting executed once, which creates a list object, which is set aside and kept around, which is why it accumulates changes if you modify it. Exactly the same thing happens with None, however None is not being modified nor is it mutable to begin with, so you're not seeing any weird side effects. None is still only being "executed" once, and that particular None value is set aside just as the list was, it's just that you're not doing anything to the None value itself.

like image 107
deceze Avatar answered Oct 27 '25 23:10

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!