Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico 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: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de rTC5LCuAZoyvu6ksKtAQ7AXMrZ9iicSkzpqTzULvbAbw== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99202 On Sat, Nov 21, 2015 at 7:38 PM, Todd 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