Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'arguments': 0.05; 'assign': 0.05; 'defaults': 0.07; 'dictionary': 0.07; 'though.': 0.07; 'python': 0.07; '>>>>': 0.09; 'attribute': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:Function': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'pm,': 0.11; '>>>': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '"a"': 0.16; '(excluding': 0.16; '(until': 0.16; '4:59': 0.16; 'around,': 0.16; 'f()': 0.16; 'patching': 0.16; 'returned,': 0.16; 'argument': 0.16; "wouldn't": 0.18; 'suggest': 0.19; 'cheers,': 0.20; 'header:In-Reply-To:1': 0.22; 'previously': 0.22; 'documented': 0.23; 'ken': 0.23; 'keys': 0.23; 'assume': 0.25; 'expect': 0.26; 'parameters': 0.26; 'instead': 0.26; '(in': 0.27; 'function': 0.27; 'changing': 0.29; 'error': 0.29; 'probably': 0.30; 'luck,': 0.31; 'url:library': 0.31; 'perhaps': 0.32; 'to:addr:python-list': 0.32; '...': 0.32; 'url:docs': 0.33; 'cases,': 0.33; 'sufficient': 0.33; 'header:X-Complaints-To:1': 0.34; 'question': 0.35; 'header:User-Agent:1': 0.35; 'quite': 0.36; 'case': 0.37; 'url:python': 0.37; 'sequence': 0.38; 'steven': 0.38; 'user': 0.38; 'url:org': 0.38; 'used': 0.38; 'received:org': 0.38; 'stable': 0.39; 'work?': 0.39; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'best': 0.60; 'making': 0.62; 'williams': 0.64; 'safe': 0.65; 'fashion': 0.65; 'important.': 0.65; 'url:3': 0.68; 'url:0': 0.69; 'guaranteed': 0.72; 'unnecessary': 0.73; 'informed': 0.84; '08:30': 0.84; 'technique.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Colin J. Williams" Subject: Re: Function __defaults__ Date: Mon, 25 Apr 2011 10:24:33 -0400 References: <4db3f444$0$29978$c3e8da3$5496439d@news.astraweb.com> <4DB458B6.3000609@seehart.com> <4DB5696A.7050905@seehart.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 206-248-183-148.dsl.teksavvy.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <4DB5696A.7050905@seehart.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303741502 news.xs4all.nl 32470 [::ffff:82.94.164.166]:40591 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3981 On 25-Apr-11 08:30 AM, Ken Seehart wrote: > On 4/25/2011 4:59 AM, Colin J. Williams wrote: >> On 24-Apr-11 13:07 PM, Ken Seehart wrote: >>> On 4/24/2011 2:58 AM, Steven D'Aprano wrote: >>>> Consider this in Python 3.1: >>>> >>>> >>>>>>> def f(a=42): >>>> ... return a >>>> ... >>>>>>> f() >>>> 42 >>>>>>> f.__defaults__ = (23,) >>>>>>> f() >>>> 23 >>>> >>>> >>>> Is this an accident of implementation, or can I trust that changing >>>> function defaults in this fashion is guaranteed to work? >>> >>> This is documented in python 3, so I would expect it to be stable (until >>> python 4, that is) >>> http://docs.python.org/py3k/whatsnew/3.0.html#operators-and-special-methods >>> >>> http://docs.python.org/py3k/library/inspect.html#types-and-members >>> >>> The f.__defaults__ attribute was previously known as f.func_defaults (in >>> python 2.x), which has been around, documented and stable for quite a >>> while. >>> >>> So it's probably just as safe as any other monkey patching technique. :) >>> >>> Best of luck, >>> Ken >>> >> >> Wouldn't it make more sense to return a dictionary instead of a tuple? >> >> Colin W. >> > > I assume you mean making the value of f.__defaults__ a dictionary > instead of a tuple. > > A dictionary would be slower to process since it would have to iterate > the dictionary keys and assign arguments by name. > Since argument defaults can only be applied to the rightmost contiguous > sequence of zero or more parameters (excluding *args,**kwargs), a tuple > is sufficient to cover all cases, so a dictionary would provide no > advantage. > Also, a dictionary would produce an unnecessary error case (if a key in > the dictionary is not the name of an argument). > > Good question though. > > Cheers, > Ken > I doubt that this functionality would be used in time critical work and so I suggest that efficiency is not a key consideration. Loss of information is perhaps more important. With the tuple being returned, the user is not informed that the value is associated with the name "a" Colin W.