Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initialization in argument definitions

1 view
Skip to first unread message

Brentt

unread,
Nov 21, 2008, 4:25:45 PM11/21/08
to
Hi, I know this is a terribly simple question, but the docs seem to be
designed for people who probably find a the answer to this question
terribly obvious. But its not at all obvious to me.

I can't figure out why when I define a function, a variable
(specifically a list) that I define and initialize in the argument
definitions, will not initialize itself every time its called. So for
example, when making a simple list of a counting sequence from num (a
range list), if I call the function multiple times, it appends the
elements to the list generated the times it was called before, even
though the variable for the list is initialized in the argument
definitions.

def foo_range(num,aList = []):
aList = []
#why is this seemingly extra initialization necessary? shouldn't it be
initialized in the argument definitions?
#but if its not there and the function is called multiple times the
elements generated (see below)
#append to the list generated before.
while num <= 10:
aList.append(num)
num +=1
else:
return aList

Why is this? Thanks, hope its not a stupid quesiton.

Chris Rebert

unread,
Nov 21, 2008, 4:34:28 PM11/21/08
to Brentt, pytho...@python.org
On Fri, Nov 21, 2008 at 1:25 PM, Brentt <Brentt...@gmail.com> wrote:
> Hi, I know this is a terribly simple question, but the docs seem to be
> designed for people who probably find a the answer to this question
> terribly obvious. But its not at all obvious to me.
>
> I can't figure out why when I define a function, a variable
> (specifically a list) that I define and initialize in the argument
> definitions, will not initialize itself every time its called. So for
> example, when making a simple list of a counting sequence from num (a
> range list), if I call the function multiple times, it appends the
> elements to the list generated the times it was called before, even
> though the variable for the list is initialized in the argument
> definitions.
>
> def foo_range(num,aList = []):
That should probably be: def foo_range(num, aList=None):

> aList = []
> #why is this seemingly extra initialization necessary? shouldn't it be
> initialized in the argument definitions?
> #but if its not there and the function is called multiple times the
> elements generated (see below)
> #append to the list generated before.
> while num <= 10:
> aList.append(num)
> num +=1
> else:
> return aList
>
> Why is this? Thanks, hope its not a stupid quesiton.

No, but it is a very frequently asked one:
http://effbot.org/zone/default-values.htm

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com

> --
> http://mail.python.org/mailman/listinfo/python-list
>

George Sakkis

unread,
Nov 21, 2008, 4:38:54 PM11/21/08
to
On Nov 21, 4:25 pm, Brentt <BrenttNew...@gmail.com> wrote:

> Hi, I know this is a terribly simple question, but the docs seem to be
> designed for people who probably find a the answer to this question
> terribly obvious. But its not at all obvious to me.

Don't worry, it's not obvious to *anyone* new to Python (and many not-
so-new for that matter).

> I can't figure out why when I define a function, a variable
> (specifically a list) that I define and initialize in the argument
> definitions, will not initialize itself every time its called. So for
> example, when making a simple list of a counting sequence from num (a
> range list), if I call the function multiple times, it appends the
> elements to the list generated the times it was called before, even
> though the variable for the list is initialized in the argument
> definitions.
>
> def foo_range(num,aList = []):
> aList = []
> #why is this seemingly extra initialization necessary? shouldn't it be
> initialized in the argument definitions?
> #but if its not there and the function is called multiple times the
> elements generated (see below)
> #append to the list generated before.
> while num <= 10:
> aList.append(num)
> num +=1
> else:
> return aList
>
> Why is this? Thanks, hope its not a stupid quesiton.

Sigh.. no it's not stupid at all; actually it is (and will probably
remain, unfortunately) the most FAQ of all times:
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

George

John Machin

unread,
Nov 21, 2008, 4:42:33 PM11/21/08
to
On Nov 22, 8:25 am, Brentt <BrenttNew...@gmail.com> wrote:
> Hi, I know this is a terribly simple question, but the docs seem to be
> designed for people who probably find a the answer to this question
> terribly obvious. But its not at all obvious to me.
>
> I can't figure out why when I define a function, a variable
> (specifically a list) that I define and initialize in the argument
> definitions, will not initialize itself every time its called.

Have you worked through the tutorial (which is part of the docs)? See
http://docs.python.org/tutorial/controlflow.html#default-argument-values
... look for "Important warning"

Terry Reedy

unread,
Nov 21, 2008, 5:41:35 PM11/21/08
to pytho...@python.org

The def statement is an executable statement that creates a function
object. When you execute the def statement, the parameter list,
including default arg object expressions, is evaluated for creating the
function object and its associated code object. The 'suite' that
follows the ':' and possible a doc string is compiled for the code object.

The language manual entry for Function definitions explains this, with
the first sentence in bold.
"Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same “pre-computed” value is used for
each call. This is especially important to understand when a default
parameter is a mutable object, such as a list or a dictionary: if the
function modifies the object (e.g. by appending an item to a list), the
default value is in effect modified."

The allows a choice of having a 'default' evaluated either when the
function is defined, which is nearly always what is wanted, or when the
function is called, by using None or another flag object, and then testing.

tjr

Egon Frerich

unread,
Nov 21, 2008, 6:00:50 PM11/21/08
to Brentt, pytho...@python.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brentt schrieb:

look up
http://docs.python.org/tutorial/controlflow.html#defining-functions

"The execution of a function introduces a new symbol table used for
the local variables of the function. More precisely, all variable
assignments in a function store the value in the local symbol table;
whereas variable references first look in the local symbol table, then
in the local symbol tables of enclosing functions, then in the global
symbol table, and finally in the table of built-in names. Thus, global
variables cannot be directly assigned a value within a function (unless
named in a global statement), although they may be referenced."

Egon

| --
| http://mail.python.org/mailman/listinfo/python-list

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJJz2iZRiDo9Iq4qIRAhTLAJ41TTysvN++TNF1tvanjAxBPhdBawCfe1tY
uDdbPYWBAEkbYNhbKQGkx88=
=9gL8
-----END PGP SIGNATURE-----

Steven D'Aprano

unread,
Nov 21, 2008, 8:51:34 PM11/21/08
to
On Fri, 21 Nov 2008 13:25:45 -0800, Brentt wrote:

> I can't figure out why when I define a function, a variable
> (specifically a list) that I define and initialize in the argument
> definitions, will not initialize itself every time its called.

Because you haven't told the function to initialize the value every time
it's called. You are laboring under a misapprehension. Function default
values are created ONCE, when you define the function.

> So for
> example, when making a simple list of a counting sequence from num (a
> range list), if I call the function multiple times, it appends the
> elements to the list generated the times it was called before, even
> though the variable for the list is initialized in the argument
> definitions.

No it isn't. You need to re-set your thinking, that's not what Python
does. Try this:

def expensive():
# simulate an expensive function call
import time
time.sleep(30)
return time.time()


def parrot(x=expensive()):
return x

The expensive call is made once only. If you want it made every time, you
have to explicitly make that call every time:

def parrot(x=None):
if x is None:
x = expensive()
return x


For bonus marks, predict the behaviour of this:

def spam():
def ham(x=expensive()):
return x
return ham()

--
Steven

Terry Reedy

unread,
Nov 21, 2008, 9:11:20 PM11/21/08
to pytho...@python.org
George Sakkis wrote:
> On Nov 21, 4:25 pm, Brentt <BrenttNew...@gmail.com> wrote:
>
>> Hi, I know this is a terribly simple question, but the docs seem to be
>> designed for people who probably find a the answer to this question
>> terribly obvious. But its not at all obvious to me.
>
> Don't worry, it's not obvious to *anyone* new to Python (and many not-
> so-new for that matter).

Speak for yourself, not for me.

greg

unread,
Nov 22, 2008, 7:13:11 PM11/22/08
to
George Sakkis wrote:

> Don't worry, it's not obvious to *anyone* new to Python (and many not-
> so-new for that matter).

That's by no means certain, because we only hear from the
people who guessed the wrong way. We have no way of knowing
how many people guessed the right way.

--
Greg

Aaron Brady

unread,
Nov 22, 2008, 8:32:03 PM11/22/08
to
On Nov 21, 3:25 pm, Brentt <BrenttNew...@gmail.com> wrote:
> Hi, I know this is a terribly simple question, but the docs seem to be
> designed for people who probably find a the answer to this question
> terribly obvious. But its not at all obvious to me.
>
> I can't figure out why when I define a function, a variable
> (specifically a list) that I define and initialize in the argument
> definitions, will not initialize itself every time its called.
snip

> Why is this? Thanks, hope its not a stupid quesiton.

That is not what the equals sign means in a 'def' statement in Python.

No, not a stupid question.

Marc 'BlackJack' Rintsch

unread,
Nov 23, 2008, 2:24:35 AM11/23/08
to

Or how many worked through the tutorial, stumbled across the warning
about that behavior and got that question answered before they have even
known there's something to guess.

Ciao,
Marc 'BlackJack' Rintsch

Terry Reedy

unread,
Nov 23, 2008, 1:03:14 PM11/23/08
to pytho...@python.org

I did not guess. I started by reading through the tutorial in about
three hours. Then I did some work with Python. Within a couple of
weeks, I read through the ref manual. I am really weird, it seems.

0 new messages