functions, list, default parameters

Mark Wooding mdw at distorted.org.uk
Sat Nov 6 08:37:42 EDT 2010


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Fri, 05 Nov 2010 12:17:00 +0000, Mark Wooding wrote:
> > Right; so a half-decent compiler can notice this and optimize
> > appropriately.  Result: negligible difference.
>
> Perhaps the biggest cost is that now your language has inconsistent 
> semantics: some function defaults are set on every call, and some are set 
> once, when the function is defined, and the choice between the two 
> happens via "magic" -- the compiler decides what to do, you don't.

The /semantics/ are indistinguishable.  The implementations are
different, and I'll grant that the performance model is more complex for
optimized DIC.

> I have mixed feelings about compiler optimizations. Things like constant 
> folding seems to be both harmless and useful, but other optimizations not 
> so much. It sets up a discrepancy between what the source code does and 
> what the compiled code does, and in the case of (say) floating point 
> code, can introduce *serious* bugs.

Oooh, careful now.  It's important not to muddle semantics-preserving
transformations (like memoizing constant immutable objects, as I'm
suggesting, or constant folding) and tolerated semantics-altering
transformations such as increasing precision of intermediate
floating-point values.  (`Tolerated' here means that some language
specifications grant specific permission for these optimizations despite
the fact that they don't preserve semantics.)

Semantics-altering optimizations are scary, to be approached only with
great trepidation.  Semantics-preserving optimizations are nearly free,
except for the potentially complicated performance model.

> >         _missing = ['missing']
>
> A curious choice for the sentinel value. We're not using Python 1.5 any 
> longer :)

Two reasons.  Firstly, this comes from my Lisp background: making a list
is the obvious way of producing an unforgeable object.  Secondly, if you
see an objects that prints as ['missing'], you have a chance of working
out where it came from; for a loose object() that's kind of hard without
a wabbit hunter.

-- [mdw]



More information about the Python-list mailing list