Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail 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; 'attributes': 0.05; '(default': 0.07; 'none:': 0.07; 'argument,': 0.09; 'attribute.': 0.09; 'func': 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; 'syntax.': 0.09; 'def': 0.13; 'subject:was': 0.15; '*after*': 0.16; '*before*': 0.16; 'baz': 0.16; 'definition.': 0.16; 'subject:=': 0.16; 'subject:Early': 0.16; 'wrote:': 0.18; 'working.': 0.18; 'header:In-Reply-To:1': 0.22; 'stops': 0.23; 'defined': 0.24; 'code': 0.25; 'skip:_ 20': 0.26; 'function': 0.27; 'do.': 0.28; 'pm,': 0.29; 'defined,': 0.30; 'words,': 0.32; 'subject:]': 0.32; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'instead': 0.33; 'done': 0.34; 'to:addr:python-list': 0.34; 'function.': 0.34; 'skip:@ 10': 0.34; 'received:au': 0.36; '...': 0.36; 'using': 0.38; 'steven': 0.38; 'received:org': 0.38; "i'd": 0.39; 'being': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'move': 0.40; 'your': 0.61; 'believe': 0.65; '07:25': 0.84; 'received:110': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Date: Sun, 25 Dec 2011 15:12:27 +1100 References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 110-175-240-90.static.tpgi.com.au User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 In-Reply-To: <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324786507 news.xs4all.nl 6970 [2001:888:2000:d::a6]:47160 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17876 On 12/24/2011 07:25 PM, Steven D'Aprano wrote: >> I'd use a function attribute. >> >> def func(x, y=None): >> if y is None: >> y = func.default_y >> ... >> func.default_y = [] >> >> That's awkward only if you believe function attributes are awkward. > > I do. All you've done is move the default from *before* the function is > defined to *after* the function is defined, instead of keeping it in the > function definition. It's still separate, and if the function is renamed > your code stops working. In other words, it violates encapsulation of the > function. Although we can solve that (default being after the function is defined) using a simple decorator: def funcargs(**args): def __decorate_with_args(func): for k,v in args.items(): setattr(func, k, v) return func return __decorate_with_args Usage: @funcargs(foo=4) def bar(baz): return baz + bar.foo et voila, we had just reinvented early binding default argument, with a much uglier syntax.