Google グループは Usenet の新規の投稿と購読のサポートを終了しました。過去のコンテンツは引き続き閲覧できます。
Dismiss

function with list argument defaulting to [] - what's going on here???

閲覧: 2 回
最初の未読メッセージにスキップ

Mike

未読、
2007/04/14 21:23:162007/04/14
To:
While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.

>>> def f(l, r = []):
for itm in l:
r.append(itm)
print r


>>> a = [1,2,3]
>>> f(a)
[1, 2, 3]
>>> f(a)
[1, 2, 3, 1, 2, 3]
>>> f(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

I know the function is quite artificial, but it's for illustration only.
Why is "r" not being reset to the empty list on subsequent calls? It
seems like it should be reinitialized when not explicitly provided.

Thanks in advance.
Mike

Troy Melhase

未読、
2007/04/14 21:33:112007/04/14
To: Mike、pytho...@python.org
On 4/14/07, Mike <mm...@woh.rr.com> wrote:
> While trying to write a recursive function involving lists, I came
> across some (to me) odd behavior which I don't quite understand. Here's
> a trivial function showing the problem.

from http://docs.python.org/ref/function.html :

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.

Mike

未読、
2007/04/14 21:49:112007/04/14
To: Troy Melhase
Thanks, Troy. I never cease to be amazed at what can be discovered by
reading the manual! <self bangs head on wall>

Mike

Steven D'Aprano

未読、
2007/04/14 22:58:532007/04/14
To:

This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.


What do people think? A misuse or good use of warnings?

--
Steven.

Alex Martelli

未読、
2007/04/14 23:07:412007/04/14
To:
Mike <mm...@woh.rr.com> wrote:
...

> Why is "r" not being reset to the empty list on subsequent calls? It
> seems like it should be reinitialized when not explicitly provided.

<http://www.python.org/doc/faq/general/#why-are-default-values-shared-be
tween-objects>


Alex

Paddy

未読、
2007/04/14 23:20:422007/04/14
To:
On Apr 15, 3:58 am, Steven D'Aprano

<s...@REMOVE.THIS.cybersource.com.au> wrote:
> On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
> > On 4/14/07, Mike <m...@woh.rr.com> wrote:
> >> While trying to write a recursive function involving lists, I came
> >> across some (to me) odd behavior which I don't quite understand. Here's
> >> a trivial function showing the problem.
>
> > fromhttp://docs.python.org/ref/function.html:

>
> > 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.
>
> This comes up so often that I wonder whether Python should issue a warning
> when it sees [] or {} as a default argument.
>
> What do people think? A misuse or good use of warnings?
>
> --
> Steven.

I wonder if it is a check done by Pylint or PyChecker?

- Paddy.

BJörn Lindqvist

未読、
2007/04/14 23:29:012007/04/14
To: Steven D'Aprano、pytho...@python.org
> This comes up so often that I wonder whether Python should issue a warning
> when it sees [] or {} as a default argument.
>
>
> What do people think? A misuse or good use of warnings?

I think Python should reevaluate the default values.

--
mvh Björn

Steven D'Aprano

未読、
2007/04/15 0:11:022007/04/15
To:

That would break code that relies on the current behaviour. That makes it
a "maybe" for Python 3.0, and an absolute "NO!!!" for Python 2.x.

--
Steven.


Alex Martelli

未読、
2007/04/15 1:03:532007/04/15
To:

If you hope to get any change in Python 3.0, your PEP had better be in
before the end of April -- that's the 3.0 deadline for PEPs.


Alex

Tim Leslie

未読、
2007/04/15 8:23:272007/04/15
To: Paddy、pytho...@python.org

It is a check done by pylint

Tim

>
> - Paddy.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

新着メール 0 件