static variables?

Erik Max Francis max at alcyone.com
Fri Nov 22 07:01:59 EST 2002


Brian McErlean wrote:

> I think this could present problems that are just as bad.  This will
> work OK for simple types like lists, but what about instances,
> classes, or extension types, some of which currently can't be copied,
> many for good reasons (eg Locks).  Just because people can get
> confused by the current behaviour doesn't mean they wouldn't by a
> different one.

I don't doubt that there are some cases, mostly exceptional cases I'd
think, where such a system (namely where the objects are stowed in the
function and then are copied each time they're needed when a default
argument is needed) would have unexpected or undesired behavior.  For
the vast majority of cases where people use default arguments, however,
I think it would suffice:  It seems to satisfy the guideline of least
violations of expectation, is quite efficient (it simply implicitly does
something that the programmer would be doing explicitly anyway), and so
on.

For truly uncopiable objects (one where copy.copy results in an
exception), presumably you'd have some reasonable stack trace that would
lead you to where the problem is.

Again, I am _not_ suggesting that this be implemented.  The current
implementation has the benefit of being extremely simple, and although
it tends to always cause confusion, it's well-entrenched and it would be
a downright bad idea to change it now.

I'm just trying to make the point that the current implementation isn't
the only relatively straightforward approach to the solution, and so
isn't a priori the "right" one.

> A better way might be to compile the expression and store off the
> bytecode, to be executed each function invocation if nothing is bound
> to the parameter - sort of an extra, optional, first line to the
> function.

This would be another example of a reasonably straightforward
implementation that would get the desired effect as well.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Physics, as we know it, will be over in six months.
\__/ Max Born (1928)
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list