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


Groups > comp.lang.python > #99202

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
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 Sat, 21 Nov 2015 19:46:17 +1100
Lines 23
Message-ID <mailman.33.1448095585.2291.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <CAFpSVpJ1MOaH=V9wxzgzkRzrSG0YoKQk7WQ4Fo1K5fd0QLPBuQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de rTC5LCuAZoyvu6ksKtAQ7AXMrZ9iicSkzpqTzULvbAbw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '21,': 0.07; 'cc:addr :python-list': 0.09; 'runs,': 0.09; 'def': 0.13; 'subject: \n ': 0.15; 'evaluated.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hacks': 0.16; 'lambda': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'ugly.': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'decorator': 0.22; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'handled': 0.29; "i'm": 0.30; 'implement': 0.32; 'maybe': 0.33; 'wrap': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'functions.': 0.35; 'nov': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'front': 0.38; 'no,': 0.38; 'received:209': 0.38; 'sure': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'leaving': 0.63; 'chrisa': 0.84; 'todd': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=RpkVRMQ/bltQSrPpYtqBmZ+Y9lfL1mDsa0G3p2Uwi00=; b=XAeVgEmIYKVQaHrkx8PkDhQQ1RowgQQpe69gST4tsFtSPJWO8Vs8+oJmSl9uii+F2w w1X5zeTTJQL9SzLJStV8R/9p9jKfcrdOTrQJqXkDnnHY2o6Qb15o42h7ZLWYLovH/EMh aZ1wy6rEs4os46S9g6O8hYJFZhAX8Y3WFjR3l98amt263R3CD3zHcrldrzanabRDV7PC 38VE+chKpFpZU3z4hZMQsFwfjvKmmhIemjDMnZTUKWqt2H3RqRacaFwDQJLFwTlMwTTA Deg0AXQVLDuU73itM27Io2AhpJ5g1kndMaKj2ECp5+qDLcSBMkKlGpmiTLxH9T73U3Tl RplA==
X-Received by 10.50.93.72 with SMTP id cs8mr5632328igb.13.1448095577534; Sat, 21 Nov 2015 00:46:17 -0800 (PST)
In-Reply-To <CAFpSVpJ1MOaH=V9wxzgzkRzrSG0YoKQk7WQ4Fo1K5fd0QLPBuQ@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99202

Show key headers only | View raw


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


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