[issue4098] surprised by default list parameter

View: New views
4 Messages — Rating Filter:   Alert me  

[issue4098] surprised by default list parameter

by STINNER Victor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


New submission from Torsten Rottmann <trott@...>:

The attached file produced the following result:

______________________
trott$ python2.5 p6.py
[]
[1234]
______________________

I expected both times the empty list since the print
statement prints just a defaulted parameter ("e") which
was defaulted to an empty list literal ("[]") and never set
by supplying an actual class parameter.

The object property "entries" appears to be shared by
both instances. Why?

----------
components: Build
files: p6.py
messages: 74624
nosy: trott
severity: normal
status: open
title: surprised by default list parameter
type: behavior
versions: Python 2.5, Python 2.5.3, Python 2.6
Added file: http://bugs.python.org/file11761/p6.py

_______________________________________
Python tracker <report@...>
<http://bugs.python.org/issue4098>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%40nabble.com


[issue4098] surprised by default list parameter

by STINNER Victor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Ulrich Eckhardt <eckhardt@...> added the comment:

http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

The same recently cropped up on the users mailinglist under the topic
"default value in __init__".

----------
nosy: +eckhardt

_______________________________________
Python tracker <report@...>
<http://bugs.python.org/issue4098>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%40nabble.com


[issue4098] surprised by default list parameter

by STINNER Victor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Torsten Rottmann <trott@...> added the comment:

OK. I've read the Python Reference Manual 7.6 "Function Definition".
It "explains" why this happens.

BUT: Why can`t the compiler make a copy of the default object, if
it's mutable (e.g. a list)? This can't be that difficult.

_______________________________________
Python tracker <report@...>
<http://bugs.python.org/issue4098>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%40nabble.com


[issue4098] surprised by default list parameter

by STINNER Victor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Amaury Forgeot d'Arc <amauryfa@...> added the comment:

Yes, the default value expression could be evaluated each time the
function is called without the parameter. But it would be another language.
Default values are computed once, and this is a Python feature.

It can be useful, for example to cache computations, or to achieve
"early binding" in nested functions, as explained in the suggested FAQ.

----------
nosy: +amaury.forgeotdarc
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report@...>
<http://bugs.python.org/issue4098>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%40nabble.com

LightInTheBox - Buy quality products at wholesale price!