Default Value

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jun 20 21:08:41 EDT 2013


On Thu, 20 Jun 2013 11:57:34 -0600, Ian Kelly wrote:

> Additionally, with late-binding semantics the default values would no
> longer be default *values*.  They would be initialization code instead,
> which sort of flies in the face of the idea that late-binding would
> somehow be better for functional programming -- if the default
> expressions have no side effects and since they don't depend on the
> function arguments, why should they need to run more than once?  If the
> goal is indeed to make the the functions more functional, then the
> proper solution would be to keep the binding early but just disallow
> mutable defaults altogether -- which is tricky to achieve in Python, so
> we simply emulate it with the advice "don't use mutable function
> defaults".


Well said!


At the risk of having twice as many gotchas, and at making the 
implementation become even more complex, I would very slightly lean 
towards allowing some sort of syntactic sugar for late binding default 
values just to shut people up about this issue. Here's my syntax plucked 
out of thin air:

def func(arg, x=expression, !y=expression):
    ...

where y=expression is late-bound, and the above is compiled to:

def func(arg, x=expression, y=None):
    if y is None:
        y = expression
    ...


Then Ranting Rick can bitch and moan about how this violates "Explicit is 
better than implicit".


-- 
Steven



More information about the Python-list mailing list