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

Larry Hastings larry at hastings.org
Sun May 10 12:14:32 CEST 2009


George Sakkis wrote:
> +0.x for a new keyword that adds dynamic semantics (and removes the
> need for the sentinel kludge).


We don't need new syntax for it.  Here's a proof-of-concept hack that 
you can do it with a function decorator.

    import copy

    def clone_arguments(f):
      default_args = list(f.func_defaults)
      if len(default_args) < f.func_code.co_argcount:
        delta = f.func_code.co_argcount - len(default_args)
        default_args = ([None] * delta) + default_args
      def fn(*args):
        if len(args) < default_args:
          args = args + tuple(copy.deepcopy(default_args[len(args):]))
        return f(*args)

      return fn

    @clone_arguments
    def thing_taking_array(a, b = []):
      b.append(a)
      return b

    print thing_taking_array('123')
    print thing_taking_array('abc')


-1 on changing Python one iota for this,


/larry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090510/953a613b/attachment.html>


More information about the Python-ideas mailing list