Why python doesn't use syntax like function(, , x) for default parameters?

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 10 10:51:01 CET 2006


Dmitry Anikin wrote:

> Is there some contradiction in python syntax which disallows
> an easy implementation of this feature, or just nobody bothered
> with this? If former is the case, please show me why, because
> I badly need this feature in embedded python app (for
> compatibility with other language that uses such syntax) and might
> venture to implement it myself, so don't want to waste time
> if it's gonna break something.
> 
I think you would find it hard to implement exactly that in Python. Of 
course what you can do easily enough is invent a magic 'missing' value and 
map Python calls of:

   a_func(missing, missing, 3)

to a call of:

   a_func(,,3)

in your other language. It seems to me though that writing:

   a_func(z=3)

ought to be clearer to anyone reading the code, especially when you come 
back to your code in 6 months time and find:

   b_func(,,,,,,,,,,,1,,,,2)


Remember Python is a dynamic language, so the compiler cannot tell which 
function you are actually calling. In a statically bound language parameter 
defaults are often restricted to constant values and the default values are 
simply inserted in place of any missing arguments when the call is 
compiled. That isn't possible in Python, so the interpreter has to insert 
the missing values when you actually make the call. The code to do that is 
complex enough with the current restrictions.

To allow arbitrary arguments to be defaulted, I think you would have to add 
a new magic value to represent a missing parameter, make the compiler pass 
that in place of any omitted positional parameters, and then make the 
function call code check all parameters to see if they are missing values 
and if so substitute the actual default. It would be possible, but it  
doesn't really give you any additional power since you can already do that 
in the cases where you need it by using keyword arguments or by passing an 
explicit value to mean 'replace this parameter by some other canned value'.



More information about the Python-list mailing list