Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) Date: Sat, 21 Nov 2015 08:01:05 -0700 Lines: 24 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ZG71YD4CBtfXCGRh7Z4VgAgVb1sdqFM+1/OlI6USco4Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; '21,': 0.07; 'runs,': 0.09; 'spelled': 0.09; 'def': 0.13; 'subject: \n ': 0.15; 'evaluated.': 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; 'decorator': 0.22; 'am,': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; '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; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'front': 0.38; 'no,': 0.38; 'received:209': 0.38; 'sure': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'leaving': 0.63; 'to:name:python': 0.84; 'todd': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=UjVhJ2XcJnnoylwSdIIL8arLgI3DYL/jpI/2f5dylyw=; b=LAX0OMDCEdA9JGigap/IXiWdQmXI1N7/CqjhhVfiSEeSQtQyEyf5PNWfJvNTKyYudA ZqVMwzwZtL2/N3P2y9KXNeh/xwRxIy2nWnyVdQjsTgxQA4H5VC/ycoGSN2Jz8zYxjQp9 ipJI2hO3F5Gf9jRh0h13U6HzK3PePenBH+mu4C6ywRbIBHaJTE0LiRpn05Jgv3mlYam5 rO69SzpQfzzJ0RA5ndEkcz1WVs6aXKVQi4VSpimIs8fbmEUDWTE6Lq6WsHJ07QgEMSgT yQwqswOk09uIIA6TjkJwLCYYvIB84blkj/f2gCHCsyX2iuOtNwhjV9i8n/Ig9BxHxHvf heLw== X-Received: by 10.107.137.226 with SMTP id t95mr18125462ioi.188.1448118105289; Sat, 21 Nov 2015 07:01:45 -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:99212 On Sat, Nov 21, 2015 at 1:46 AM, Chris Angelico wrote: > 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. Note that "lambda: []" can also be spelled "list".