initialization in argument definitions
george.sakkis at gmail.com
Fri Nov 21 22:38:54 CET 2008
On Nov 21, 4:25 pm, Brentt <BrenttNew... at 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
> 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:
> 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:
More information about the Python-list