side effects on *some* default parameters

Jeremy Fincher tweedgeezer at hotmail.com
Fri Feb 14 01:08:38 EST 2003


Joe Grossberg <jgrossberg at matrixgroup.net> wrote in message news:<mailman.1045154045.19349.python-list at python.org>...
> In other words, I'm not asking "Does Python do this?" -- I'm asking "Why does Python do this?"

I think one of the posters was right on target when he said that it's
easier to implement "evaluate every time" behavior via "evaluate once"
behavior than it is to implement "evaluate once" behavior via
"evaluate every time" behavior.

Compare:

# Implementing evaluate-many-times via evaluate-once.
def aFunction(aList=None):
    if aList is None:
        aList = []
    ...

and:

# implementing evaluate-once via evaluate-many-times.
_aList = []
def aFunction(aList=None):
    if aList is None:
        aList = _aList
    ...

It keeps the namespace cleaner.  And there *are* cases in which the
"evaluate once" behavior is desired -- specifically, whenever a
programmer wants mutable variables that are local to the function.

Jeremy




More information about the Python-list mailing list