On Fri, Nov 21, 2008 at 1:25 PM, Brentt <[EMAIL PROTECTED]> 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 > -- http://mail.python.org/mailman/listinfo/python-list