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


Groups > comp.lang.python > #99212

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

From Ian Kelly <ian.g.kelly@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 08:01 -0700
Message-ID <mailman.38.1448118113.2291.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <CAFpSVpJ1MOaH=V9wxzgzkRzrSG0YoKQk7WQ4Fo1K5fd0QLPBuQ@mail.gmail.com> <CAPTjJmpP2j4v4DiY_wdp70rcTCFkjkXpkp9J4UhDZ388Lxvq+w@mail.gmail.com>

Show all headers | View raw


On Sat, Nov 21, 2015 at 1:46 AM, Chris Angelico <rosuav@gmail.com> wrote:
> 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.

Note that "lambda: []" can also be spelled "list".

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-21 08:01 -0700

csiph-web