-1

I'm trying to understand this question and answers:

python function default parameter is evaluated only once?

in order to understand it i try:

def f1(a, L=[]):
    if not L:
        print "L is empty"
        L = []
    L.append(a)
    return L

>>>f1(1)
L is empty
[1]
>>>f1(1)
L is empty
[1]

def f2(a, L=[]):
        if L:
            print "L isn't empty"
            L = []
        L.append(a)
        return L

>>>f2(1)
[1]
>>>f2(1)
L isn't empty
[1]

So I think in the case of f1 L is becoming empty again every time - it is assigned to [] again after every call of f1. But in case of f2 L is somehow isn't empty? Why?

2
  • Perhaps you can make L a global list? Commented Jan 16, 2015 at 23:16
  • I can, but don't want. All I want is to understand why is behavior of this functions is so strange Commented Jan 17, 2015 at 22:02

1 Answer 1

6

When you do this:

L = []

...you don't change the value referenced by L; you change the reference L to point to a brand new list [].

If you want to empty the list referenced by L without changing the reference itself, you can write:

del L[:]

...or use methods such as remove or pop that work by changing the current list.


Here's f1 and f2 in animated form to help you understand. (Click for webm. Wait for each gif to fade to white. The gifs don't sync up, sorry.)

f1

f2

Sign up to request clarification or add additional context in comments.

6 Comments

Can you explain why in case of f2 i've got non empty list, but in case of f1 it's always emplty though code in both functions is similar?
@user3572950 in f1(x), L=[], thus you run the if not L branch, which discards the L reference, so next time you run f1(y), it'll still start with L=[]. When you call f2(x) the first time, L=[], so you skip the if L branch and go straight to changing L with L.append. When you then call f2(y), L=[x], so you go through the if L branch (and discard the L reference).
@user yep, that's it: when you assign to L you assign a new value to the reference L without affecting the previous value. Consider this: A=B=[]; A.append(0); print A, B; A=[]; A.append(1); B.append(2); print A, B; this should give [0] [0] [1] [0, 2]
Maybe I'm starting to understand - there are two "versions" of L in each function - one "version" is default variable, evaluated only once, and other "version" - new list, that i'm creating inside if statement as you said you change the reference L to point to a brand new list [] But it's unclear to me why So next time you run f1(y), it'll still start with L=[] By this time, when i run f1 second time there are two "versions" of L - one is equal [] and another is equal [1] , so how can if statement understand what "version" of L it should use?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.