YAI: syntax for preset locals without using dummy args with d efaults

Delaney, Timothy tdelaney at avaya.com
Sun Jan 12 20:09:53 EST 2003


> From: Skip Montanaro [mailto:skip at pobox.com]
> 
>     Bengt> Right now we often see
> 
>     Bengt>     def foo(x, y=default, z=some_expression):
>     Bengt>         ...
> 
> This is just a performance hack and should be addressed by 
> other means,
> e.g., more efficient access to global variables.  See, for 
> example, PEPs 266
> and 267.  I believe other not-yet--PEPped proposals have been 
> floated as
> well.

Well, in *some* circumstances (very rare) it's not just a performance hack.

Specifically, if globals() goes away for some reason, you may not be able to
look up the name anymore. Binding it to via a default value allows you to
keep the reference.

In nearly every case though, doing this is a Bad Thing(TM). The *only* case
I've ever seen this is in my coverage module, where I blow everything away
in order to set up a "clean" environment for the module to be covered and
restore it afterwards. The module to be covered also thinks it's the
__main__ module ... ;) Plus the default values are *also* a performance
hack.

Tim Delaney





More information about the Python-list mailing list