problem with the default argument values in a function

This is the place for queries that don't fit in any of the other categories.

problem with the default argument values in a function

Postby datus » Mon Aug 19, 2013 8:16 am

text from python v2.7.5 documentation:>

Important warning: The default argument value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:

Code: Select all
def f(a, L=[]):
    L.append(a)
    return L

print f(1)
print f(2)
print f(3)

This will print

[1]
[1, 2]
[1, 2, 3]
If you don’t want the default to be shared between subsequent calls, you can write the function like this instead:

Code: Select all
def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L



i didn't get the second part...
As they say default value is evaluated only once and since lists are mutable....in the second part after one iteration, since L is not None now, it will surpass if statement and a will be appended onto L of last iteration......but this is not happening......why???
datus
 
Posts: 6
Joined: Mon Jul 08, 2013 4:37 pm

Re: problem with the default argument values in a function

Postby Mekire » Mon Aug 19, 2013 8:52 am

It is a bit hard to explain but it is a big gotcha.

If you pass an empty list (or any mutable data type as a default argument) it will only create one object. Every time you use the function the same list will be used even though generally the intent of the programmer is to initialize an empty list every call.

In the version where the default argument is None, a new list gets created in the body of the function every time you call it. In the version where the default value is [] this doesn't happen. In the latter case there is only one list despite how many times you call the function.

Basically, even if you don't really understand the reasoning, the lesson is; don't use mutable data types for default arguments. Do it the way they showed you in the second version.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with the default argument values in a function

Postby stranac » Mon Aug 19, 2013 11:53 am

Let me try to explain it.

In the first example:
  • When python gets to the function definition, a list is created - this will be used as a default argument for L
  • The function is called without an argument for L
  • The default list is used
  • 1 is appended to the list
  • When the function is called again, the default list already contains 1
  • 2 gets appended to the default list
  • ...

In the second example:
  • None is used as a default argument
  • The function is called without an argument for L - the default gets used
  • Since L is indeed None, a new list is created and assigned to L
  • 1 is appended to that list
  • On the next call, None is again used as a default value
  • Since L is None, a new empty list is created
  • 2 is appended to that list
  • ...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: problem with the default argument values in a function

Postby datus » Mon Aug 19, 2013 3:38 pm

@stranac \m/
got it!!!
datus
 
Posts: 6
Joined: Mon Jul 08, 2013 4:37 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], W3C [Linkcheck] and 3 guests