[Python-ideas] Default arguments in Python - the return

Carl Johnson cmjohnson.mailinglist at gmail.com
Sun May 10 04:06:06 CEST 2009


I think this is a case where there are pros and cons on both sides.
There are a lot of pros to the current behavior (performance,
flexibility, etc.), but it comes with the con of confusing newbies and
making people go through the same song and dance to set a  "sentinel
value" when the want the other behavior and they can't ensure that
None won't be passed. The newbie problem can't be fixed from now until
Python 4000, since it would break a lot of existing uses of default
values, but we could cut down on the annoyance of setting and check a
sentinel value by introducing a new keyword, eg.

def f(l=fresh []):
   ...

instead of

__blank = object()
def f(l=__blank):
   if l is __blank:
       l = []
   ...

The pros of a new keyword are saving 3 lines and being more clear
upfront about what's going on with the default value . The con is that
adding a new keyword bloats the language. We could try reusing an
existing keyword, but none of the current ones seem to fit:

and                 elif                import              return
as                  else                in                  try
assert              except              is                  while
break               finally             lambda              with
class               for                 not                 yield
continue            from                or
def                 global              pass
del                 if                  raise

(I copied this from Python 3.0's help, but there seems to be a
documentation error: nonlocal, None, True, and False are also keywords
in Python 3+.)

The best one on the current list it seems to me would be "else" as in

def f(l else []):
   ...

But I dunno… It just not quite right, you know?

So, I'm -0 on changing the current behavior, but I'm open to it if
someone can find a way to do it that isn't just an ad hoc solution to
this one narrow problem but has a wider general use.



More information about the Python-ideas mailing list