initialization in argument definitions

B

Brentt

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.
 
C

Chris Rebert

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[/QUOTE]
 
G

George Sakkis

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
 
J

John Machin

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"
 
T

Terry Reedy

Brentt said:
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.

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
 
E

Egon Frerich

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

-----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-----
 
S

Steven D'Aprano

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()
 
G

greg

George said:
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.
 
A

Aaron Brady

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.
 
M

Marc 'BlackJack' Rintsch

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
 
T

Terry Reedy

greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top