[Python-3000] Cleaning up argument list parsing (was Re: More wishful thinking)

Ian Bicking ianb at colorstudy.com
Tue Apr 18 02:53:03 CEST 2006


Guido van Rossum wrote:
> Here's a related but more complicated wish: define a function in such
> a way that certain parameters *must* be passed as keywords, *without*
> using *args or **kwds. This may require a new syntactic crutch.

Perhaps:
 def func(x, *args=(), foo=1): ...
Well, that doesn't really relate at all to what = means in that 
position. "def func(x, *None, foo=1)"? "def func(x, **, foo=1)"... oh, 
well why not just "def func(x, &optional foo=1)".
Maybe related is a way of telling if the keyword was passed at all. In 
other words, a replacement for:
 sentinal = []
 def func(x, y=sentinal):
 if y is sentinal:
 # calculate from x
Or, without any danger of a misused sentinal:
 def func(x, *args, **kw):
 if args:
 assert not kw
 assert len(args) == 1
 y = args[0]
 elif kw:
 y = kw.pop('y')
 assert not kw
 else:
 # calculate from x
But the second gives a ruined signature, and a proper implementation has 
error messages that aren't assertion errors, and so would take much 
longer to write. But I can't even think of stupid syntax for the 
latter; or I guess I can't think of useful semantics. The best I can 
think of (ignoring syntax):
 def func(x, [y=sentinal]):
 try:
 y
 except NameError:
 # calculate from x
... which is annoying, but maybe better than the alternatives. Well, 
I'm not sure if it is actually better than just using a sentinal, which 
generally works just fine.
-- 
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org


More information about the Python-3000 mailing list

AltStyle によって変換されたページ (->オリジナル) /