Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99202 > unrolled thread

Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?)

Started byChris Angelico <rosuav@gmail.com>
First post2015-11-21 19:46 +1100
Last post2015-11-21 19:46 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) Chris Angelico <rosuav@gmail.com> - 2015-11-21 19:46 +1100

#99202 — Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?)

FromChris Angelico <rosuav@gmail.com>
Date2015-11-21 19:46 +1100
SubjectRe: Late-binding of function defaults (was Re: What is a function parameter =[] for?)
Message-ID<mailman.33.1448095585.2291.python-list@python.org>
On Sat, Nov 21, 2015 at 7:38 PM, Todd <toddrjen@gmail.com> wrote:
> Rather than a dedicated syntax, might this be something that could be
> handled by a built-in decorator?
>
> Maybe something like:
>
> @late_binding
> def myfunc(x, y=[]):

No, it can't; by the time the decorator runs, the expression has
already been evaluated. Without syntax, this can only be done with
gross hacks like lambda functions.

It could be done thus:

@late_binding
def myfunc(x, y=lambda: []):

and then the decorator could wrap the function. I'm not entirely sure
I could implement it reliably, but even leaving that aside, having to
put "lambda:" in front of everything is pretty ugly.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web