On Sun, 31 Jan 2010 20:58:50 -0800, keakon wrote:
> I've found strange performance issue when using default value, the test
> code is list below:
>
> from timeit import Timer
>
> def f(x):
> y = x
> y.append(1)
> return y
>
> def g(x=[]):
> y = []
> y.append(1)
> return y
>
> def h
keakon wrote:
> The default value is mutable, and can be reused by all each call.
> So each call it will append 1 to the default value, that's very
> different than C++.
Being different from C++ is one of the many reasons some of us choose
Python ;)
This tends to bite most newcomers, so it's men
On 2月1日, 下午1时20分, alex23 wrote:
> alex23 wrote:
> > keakon wrote:
> > > def h2(x=[]):
> > > y = x
> > > y.append(1)
> > > return y + []
>
> > Are you aware that 'y = x' _doesn't_ make a copy of [], that it
> > actually points to the same list as x?
>
> Sorry, I meant to suggest trying the
alex23 wrote:
> keakon wrote:
> > def h2(x=[]):
> > y = x
> > y.append(1)
> > return y + []
>
> Are you aware that 'y = x' _doesn't_ make a copy of [], that it
> actually points to the same list as x?
Sorry, I meant to suggest trying the following instead:
def h2(x=None):
if x is None:
On Sun, Jan 31, 2010 at 8:58 PM, keakon wrote:
> I've found strange performance issue when using default value, the
> test code is list below:
>
> from timeit import Timer
>
> def f(x):
> y = x
> y.append(1)
> return y
>
> def g(x=[]):
> y = []
> y.append(1)
> return y
>
> def h(x=[]):
> y
keakon wrote:
> def h2(x=[]):
> y = x
> y.append(1)
> return y + []
> h2() is about 42 times slower than h2([]), but h() is a litter faster
> than h([]).
Are you aware that 'y = x' _doesn't_ make a copy of [], that it
actually points to the same list as x?
My guess is that the slowdown yo
I've found strange performance issue when using default value, the
test code is list below:
from timeit import Timer
def f(x):
y = x
y.append(1)
return y
def g(x=[]):
y = []
y.append(1)
return y
def h(x=[]):
y = x
y.append(1)
return y
def f2(x):
y = x
y.append(1)
return