Default Value

Roy Smith roy at panix.com
Thu Jun 20 21:40:11 EDT 2013


In article <mailman.3647.1371777895.3114.python-list at python.org>,
 Tim Chase <python.list at tim.thechases.com> wrote:

> On 2013-06-21 01:08, Steven D'Aprano wrote:
> > 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
> >     ...
> 
> I think it would have to translate to something like
> 
>   _temp_semaphore = object()
>   def func(arg, x=expression, y=_temp_semaphore):
>     if y is _temp_semaphore:
>       y = expression
> 
>     ...
> 
> because there are times you want to be able to pass None as a legit
> value in such a case (speaking from the trenches after getting stung
> by exactly that case on multiple occasions).

You can go around and around on that one.  Whatever distinguished value 
you set aside to mean "no argument passed", somebody will come up with a 
use case for why they might want to pass that as a real value.

If you really want to avoid then, then you need to go to something like:

def func(arg, **kwargs):
    if 'x' not in kwargs:
        kwargs['x'] = expression

and so on.  But that's klunky enough, I'm willing to accept the in-band 
signalling consequences of using None as a sentinel.



More information about the Python-list mailing list