YAI: syntax for preset locals without using dummy args with defaults

Andrew Dalke adalke at mindspring.com
Fri Jan 10 18:36:54 CET 2003


Bengt Richter wrote:
>     def foo(x, y=default, z=some_expression):
>         ...
> 
> where the purpose is not to have three parameters, but two
> plus a local binding (z) in foo to the value of some_expression
> evaluated in the def-time environment. z may bind something not
> accessible later at call time, or it may be just to avoid recalculating
> an expensive expression at each function call.

I offer the following to be able to compare this proposal with
what I've done to solve this problem


def _foo(x, y, z = some_expresssion):
   ....

def foo(x, y = default):
   _foo(x, y)

However, I do this only rarely (mostly for recursive functions)
since it doesn't gain all that much over

_default_z = some_expression

def foo(x, y = default):
   z = _default_z
   ...



					Andrew
'					dalke at dalkescientific.com





More information about the Python-list mailing list