Mutable default function parameter warning
Hello list, The documentation states that the default value of function parameter, if mutable, can change it's default value at runtime due to be evaluated only once on function object creation. I would like to suggest the inclusion of an default language warning when this kind of construction is used, as it's Python specific behavior and can lead to "strange behavior" or misuse by programmers that are migrating from other languages to Python. This proposal was first open as a suggestion issue in bug track, but, as a request from Mr. Peterson, I'm rewriting it to this list. http://bugs.python.org/issue9646 Regards, -- .:''''':. .:' ` Sérgio Surkamp | Gerente de Rede :: ........ sergio@gruposinternet.com.br `:. .:' `:, ,.:' *Grupos Internet S.A.* `: :' R. Lauro Linhares, 2123 Torre B - Sala 201 : : Trindade - Florianópolis - SC :.' :: +55 48 3234-4109 : ' http://www.gruposinternet.com.br
Hello I think it’s more the realm of code checkers like pyflakes, pychecker or pylint. From Python’s viewpoint, the behavior is perfectly legal and documented. Regards
On Mon, Aug 23, 2010 at 10:04 AM, Éric Araujo <merwok@netwok.org> wrote:
I think it’s more the realm of code checkers like pyflakes, pychecker or pylint. From Python’s viewpoint, the behavior is perfectly legal and documented.
+1 -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
On 8/23/2010 10:55 AM, Sérgio Surkamp wrote:
Hello list,
The documentation states that the default value of function parameter, if mutable, can change it's default value at runtime due to be evaluated only once on function object creation.
I would like to suggest the inclusion of an default language warning when this kind of construction is used, as it's Python specific behavior and can lead to "strange behavior" or misuse by programmers that are migrating from other languages to Python.
I am opposed to this for multiple reasons. 1. Static checking is for checking programs, not the compiler. IDLE currently has a Run syntax check option. I believe there have be proposals to extend that to running external checking programs. I would be in favor of that. 2. Dynamic warning are already done -- by eventually raising an exception. If no exception is raised, then code is legal and the result *might* be correct. 3. There are numerous Python features that *might* be incorrect. For instance, reuse of builtin names. This is a long, slippery, slope. 4. Defining 'ismutable' is even harder than defining 'iscallable'.
This proposal was first open as a suggestion issue in bug track, but, as a request from Mr. Peterson, I'm rewriting it to this list. http://bugs.python.org/issue9646
Please close the issue when the tracker is working again. -- Terry Jan Reedy
Terry Reedy wrote:
On 8/23/2010 10:55 AM, Sérgio Surkamp wrote:
Hello list,
The documentation states that the default value of function parameter, if mutable, can change it's default value at runtime due to be evaluated only once on function object creation.
I would like to suggest the inclusion of an default language warning when this kind of construction is used, as it's Python specific behavior and can lead to "strange behavior" or misuse by programmers that are migrating from other languages to Python.
I am opposed to this for multiple reasons.
1. Static checking is for checking programs, not the compiler. IDLE currently has a Run syntax check option. I believe there have be proposals to extend that to running external checking programs. I would be in favor of that. 2. Dynamic warning are already done -- by eventually raising an exception. If no exception is raised, then code is legal and the result *might* be correct. 3. There are numerous Python features that *might* be incorrect. For instance, reuse of builtin names. This is a long, slippery, slope. 4. Defining 'ismutable' is even harder than defining 'iscallable'.
Same here... there's a common idiom used in Python code for function localized globals which the above would break: """ # Cache used for func() _func_cache = {} # Note: func_cache may be overridden with a caller private # cache, default is to use the module global cache. def func(a, b, c, func_cache=_func_cache): ... or: # Speed up func() by localizing len and str globals/builtins def func(a, b, c, len=len, str=str): ... code using len() and str() ... """ The first is a perfectly legitimate use case. The latter is not nice, but often needed to shortcut lookups for globals and builtins when used in tight loops. There have been numerous proposals on how to solve this, but none have made it into the core.
This proposal was first open as a suggestion issue in bug track, but, as a request from Mr. Peterson, I'm rewriting it to this list. http://bugs.python.org/issue9646
Please close the issue when the tracker is working again.
-- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Aug 23 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
participants (6)
-
Daniel Stutzbach
-
M.-A. Lemburg
-
Masklinn
-
Sérgio Surkamp
-
Terry Reedy
-
Éric Araujo