Discussion:
initialization in argument definitions
greg
2008-11-23 00:13:11 UTC
Permalink
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
Terry Reedy
2008-11-23 18:03:14 UTC
Permalink
Post by greg
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.
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.
Terry Reedy
2008-11-22 02:11:20 UTC
Permalink
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.
John Machin
2008-11-21 21:42:33 UTC
Permalink
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"
Aaron Brady
2008-11-23 01:32:03 UTC
Permalink
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.
George Sakkis
2008-11-21 21:38:54 UTC
Permalink
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.
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.
aList.append(num)
num +=1
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
Steven D'Aprano
2008-11-22 01:51:34 UTC
Permalink
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
Brentt
2008-11-21 21:25:45 UTC
Permalink
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.
Terry Reedy
2008-11-21 22:41:35 UTC
Permalink
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.
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.
aList.append(num)
num +=1
return aList
Why is this? Thanks, hope its not a stupid quesiton.
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
Chris Rebert
2008-11-21 21:34:28 UTC
Permalink
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.
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.
aList.append(num)
num +=1
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
Egon Frerich
2008-11-21 23:00:50 UTC
Permalink
Brentt schrieb:
| 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.



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

Marc 'BlackJack' Rintsch
2008-11-23 07:24:35 UTC
Permalink
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.
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
Continue reading on narkive:
Loading...