Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99202
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) |
| Date | 2015-11-21 19:46 +1100 |
| Message-ID | <mailman.33.1448095585.2291.python-list@python.org> (permalink) |
| References | <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <CAFpSVpJ1MOaH=V9wxzgzkRzrSG0YoKQk7WQ4Fo1K5fd0QLPBuQ@mail.gmail.com> |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
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
csiph-web