What value should be passed to make a function use the default argument value?

Paul Rubin http
Wed Oct 4 06:23:36 EDT 2006


Antoon Pardon <apardon at forel.vub.ac.be> writes:
> Now in this case you could start by assigning arg the value 1 and
> eliminate the if test. However that only works if you know the
> default value for the argument. What he seems to be asking for
> is if there is an object, (let as call it Default), that would
> make code like:
> 
>   def f(var=1):
> 
> Equivallent to:
> 
>   def f(var=Default)
>     if var is Default)
>       var = 1

Oh, I see.  Yes, the OP should just use a distinct default value
instead of 1.  I usually do this with

   sentinel = object()

   def f(var=sentinel):
     if var is sentinel:
       # f was called without an arg 



More information about the Python-list mailing list