default arguments newbie question

Aahz Maruch aahz at panix.com
Wed Jan 10 16:21:46 EST 2001


In article <3a5cccca.77467843 at localhost>,
Victor Muslin <victor at prodigy.net> wrote:
>
>If I want a function to print "[1]" every time it is called with no
>arguments, do I have to do something like:
>
>def f(l=None):
>  if l == None:
>    print [1]
>  else:
>    print l.append(1)

Yup.  Technically, you really want to write "if l is None", though; if
you don't actually care about it being precisely None, the common idiom
is to write "if l".

Mutable default arguments are similar in some ways to static variables in
C/C++.  They are created at compile time, not execution time.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Sometimes, you're not just out of left field, you're coming in all the
way from outer space.  --Aahz



More information about the Python-list mailing list