Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: functions, list, default parameters

Reply
Thread Tools

Re: functions, list, default parameters

 
 
Chris Rebert
Guest
Posts: n/a
 
      10-21-2010
On Thu, Oct 21, 2010 at 2:36 PM, Sean Choi <(E-Mail Removed)> wrote:
> I found two similar questions in the mailing list, but I didn't understand
> the explanations.
> I ran this code on Ubuntu 10.04 with Python 2.6.5.
> Why do the functions g and gggg behave differently? If calls gggg(3) and
> g(3) both exit their functions in the same state, why do they not enter in
> the same state when I call gggg(4) and g(4)?
>
> # ----------------------------------------------------------------------*my
> code:
> def gggg(a, L=[]):


This is a common newbie stumbling-block: Don't use lists (or anything
mutable) as default argument values (in this case, for L); a new list
is *not* created for every function invocation, they'll all share the
*exact same list object*. Use None and then create a fresh list (or
what have you) in the function body. See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm

> ** *print "enter function"
> ** *print "a = ", a, "and L = ", L
> ** *if L == []:
> ** * * *print "hey, L is empty"
> ** * * *L = []


The previous line is why the two functions' behaviors differ (g()
lacks this line). Read the above FAQ, and then carefully trace through
the execution of the functions; the difference will then be clear.

Cheers,
Chris
--
http://blog.rebertia.com

> ** *L.append(a)
> ** *print "after append, L = ", L
> ** *return L


> def g(a, L=[]):
> ** *print "enter function"
> ** *print "a = ", a, "and L = ", L
> ** *if L == []:
> ** * * *print "hey, L is empty"
> ** *L.append(a)
> ** *print "after append, L = ", L
> ** *return L

 
Reply With Quote
 
 
 
 
John Nagle
Guest
Posts: n/a
 
      10-22-2010
On 10/21/2010 2:51 PM, Chris Rebert wrote:
> On Thu, Oct 21, 2010 at 2:36 PM, Sean Choi<(E-Mail Removed)> wrote:
>> I found two similar questions in the mailing list, but I didn't understand
>> the explanations.
>> I ran this code on Ubuntu 10.04 with Python 2.6.5.
>> Why do the functions g and gggg behave differently? If calls gggg(3) and
>> g(3) both exit their functions in the same state, why do they not enter in
>> the same state when I call gggg(4) and g(4)?
>>
>> # ---------------------------------------------------------------------- my
>> code:
>> def gggg(a, L=[]):

>
> This is a common newbie stumbling-block: Don't use lists (or anything
> mutable) as default argument values


That really should be an error.

John Nagle
 
Reply With Quote
 
 
 
 
Andreas Waldenburger
Guest
Posts: n/a
 
      10-22-2010
On Thu, 21 Oct 2010 19:53:53 -0700 John Nagle <(E-Mail Removed)> wrote:

> On 10/21/2010 2:51 PM, Chris Rebert wrote:
> > On Thu, Oct 21, 2010 at 2:36 PM, Sean Choi<(E-Mail Removed)> wrote:
> >> I found two similar questions in the mailing list, but I didn't
> >> understand the explanations.
> >> I ran this code on Ubuntu 10.04 with Python 2.6.5.
> >> Why do the functions g and gggg behave differently? If calls
> >> gggg(3) and g(3) both exit their functions in the same state, why
> >> do they not enter in the same state when I call gggg(4) and g(4)?
> >>
> >> #
> >> ----------------------------------------------------------------------
> >> my code: def gggg(a, L=[]):

> >
> > This is a common newbie stumbling-block: Don't use lists (or
> > anything mutable) as default argument values

>
> That really should be an error.
>

What do you mean? That using a list as default arguments should throw
an error?

While not very commonly needed, why should a shared default argument be
forbidden?

/W

--
To reach me via email, replace INVALID with the country code of my home
country. But if you spam me, I'll be one sour Kraut.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-22-2010
On Thu, 21 Oct 2010 19:53:53 -0700, John Nagle wrote:

>> This is a common newbie stumbling-block: Don't use lists (or anything
>> mutable) as default argument values

>
> That really should be an error.


No it shouldn't. Punishing everybody for a newbie mistake that nobody
makes twice would be the error.

Default mutable arguments have their place, and there is an easy work-
around for anyone who wants default arguments to be created on function
invocation rather than function definition.



--
Steven
 
Reply With Quote
 
Gregory Ewing
Guest
Posts: n/a
 
      10-29-2010
John Nagle wrote:
> On 10/21/2010 2:51 PM, Chris Rebert wrote:


>> This is a common newbie stumbling-block: Don't use lists (or anything
>> mutable) as default argument values


> That really should be an error.


No, it shouldn't. The criterion isn't whether the object is
mutable, but whether you actually mutate it. If you treat it
as read-only, there's no problem.

Since not all Python data types have immutable variants,
sometimes it's necessary to do this.

--
Greg
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-29-2010
On Fri, 29 Oct 2010 21:24:23 +1300, Gregory Ewing wrote:

> John Nagle wrote:
>> On 10/21/2010 2:51 PM, Chris Rebert wrote:

>
>>> This is a common newbie stumbling-block: Don't use lists (or anything
>>> mutable) as default argument values

>
>> That really should be an error.

>
> No, it shouldn't. The criterion isn't whether the object is mutable, but
> whether you actually mutate it. If you treat it as read-only, there's no
> problem.


And how does Python know whether some arbitrary default object is mutable
or not?

> Since not all Python data types have immutable variants, sometimes it's
> necessary to do this.



And sometimes you want the behaviour as it is.


--
Steven
 
Reply With Quote
 
Gregory Ewing
Guest
Posts: n/a
 
      10-30-2010
Steven D'Aprano wrote:

> And how does Python know whether some arbitrary default object is mutable
> or not?


It doesn't, that's the whole point.

--
Greg
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-30-2010
On Sat, 30 Oct 2010 19:31:53 +1300, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>
>> And how does Python know whether some arbitrary default object is
>> mutable or not?

>
> It doesn't, that's the whole point.


I think we're in violent agreement



--
Steven
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      11-02-2010
In message <(E-Mail Removed)>, Andreas Waldenburger
wrote:

> While not very commonly needed, why should a shared default argument be
> forbidden?


Because it’s safer to disallow it than to allow it.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      11-02-2010
In message <(E-Mail Removed)>, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>
>> And how does Python know whether some arbitrary default object is mutable
>> or not?

>
> It doesn't, that's the whole point.


Of course it knows. It is the one defining the concept in the first place,
after all.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Mixing default and non-default parameters in Constructor? desktop C++ 2 06-06-2007 08:25 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments