By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,629 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,629 IT Pros & Developers. It's quick & easy.

initialization in argument definitions

P: n/a
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.
Nov 21 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, Nov 21, 2008 at 1:25 PM, Brentt <Br**********@gmail.comwrote:
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
Nov 21 '08 #2

P: n/a
On Nov 21, 4:25*pm, Brentt <BrenttNew...@gmail.comwrote:
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/genera...etween-objects

George
Nov 21 '08 #3

P: n/a
On Nov 22, 8:25*am, Brentt <BrenttNew...@gmail.comwrote:
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/cont...rgument-values
.... look for "Important warning"

Nov 21 '08 #4

P: n/a
Brentt 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 = []):
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

Nov 21 '08 #5

P: n/a
-----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/cont...ning-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++TNF1tvanjA xBPhdBawCfe1tY
uDdbPYWBAEkbYNhbKQGkx88=
=9gL8
-----END PGP SIGNATURE-----
Nov 21 '08 #6

P: n/a
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
Nov 22 '08 #7

P: n/a
George Sakkis wrote:
On Nov 21, 4:25 pm, Brentt <BrenttNew...@gmail.comwrote:
>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.

Nov 22 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.