variable & scoping question.
steven at REMOVE.THIS.cybersource.com.au
Tue Aug 11 08:51:28 CEST 2009
On Mon, 10 Aug 2009 08:46:17 -0700, Cornelius Keller wrote:
> On 10 Aug., 17:12, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Cornelius Keller wrote:
> Ok thank you.
> I' understand now why.
> I still think this is very confusing, because default values don't
> behave like most people would expect without reading the docs.
Really? How do you expect the default value to behave in this example?
>>> import time
>>> def test(x=time.time()):
... print x
You get the same default object each time you call the function, NOT a
fresh one created. I'm sure I'd be terribly confused if Python re-
evaluated the default value each time I called the function.
There's no difference between this and the case x=, except that lists
are mutable and floats aren't. You get the same default list each time,
it just has different stuff in it. The alternative would be to get a
different list each time, and that would require re-evaluating the
default each time the function was called, which is horrible.
More information about the Python-list