Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'subject:: [': 0.03; 'subject:Python': 0.05; 'subject:skip:s 10': 0.05; 'assign': 0.07; 'exist,': 0.07; 'positional': 0.09; 'unexpectedly': 0.09; 'way:': 0.09; 'def': 0.10; 'language': 0.14; 'sat,': 0.15; "skip:' 30": 0.15; 'discussed.': 0.16; 'expert.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead:': 0.16; 'scope.': 0.16; 'subject:default': 0.16; 'subject:ideas': 0.16; 'subject:variable': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance,': 0.17; 'variables': 0.17; 'subject:] ': 0.19; 'variable': 0.20; 'parameters': 0.20; 'not,': 0.21; 'facility': 0.22; 'raise': 0.24; 'pass': 0.25; 'least': 0.25; 'header:In- Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'idea,': 0.29; 'error': 0.30; 'asking': 0.32; "skip:' 20": 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'massive': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'wanted': 0.36; 'why': 0.37; 'received:209': 0.37; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'more': 0.63; '2013': 0.84; 'this;': 0.91; 'whereby': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=nc2QhlmBDoLP6rz6BZQEsOLDYF3e+xo0jdz7gqhWK+Q=; b=Cr2zx7X2Neqz73rcBz09JGpe3BbNE3Wrx3XOOEwFkwhz16rxf6XE4nNrhIzTYONpOi H79UOdS9qEPTTqtrGKO3AwBhIEw3V/jgp/YkeI5YWd20QtFAkHabZ1ITyySoPNf+ieKC 2lK3BlFaHZkV0Ag9aNNvIHLGpmVrrqLHDBDaCP8/X5lEC3FYvgho3TrHSabvhEJDbQAl v+dubDT/OISBGzsK+WPd4F/HRpoqoX8kitT3c2XIf3GbexZubALF336davgwOYuttQi6 uW0pVS8YAgtaFIWOvYIU9gdoSfVa644OB3WnCGpjrdZKdWA6HYQUgbwq3u6VT0onC0P1 kvVw== MIME-Version: 1.0 X-Received: by 10.52.67.164 with SMTP id o4mr3721603vdt.42.1362157639858; Fri, 01 Mar 2013 09:07:19 -0800 (PST) In-Reply-To: References: Date: Sat, 2 Mar 2013 04:07:19 +1100 Subject: Re: [Python-ideas] string.format() default variable assignment From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362157649 news.xs4all.nl 6935 [2001:888:2000:d::a6]:44033 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40274 On Sat, Mar 2, 2013 at 3:55 AM, =EA=B9=80=EC=9A=A9=EB=B9=88 wrote: > why we bother with '{variable}'.format(variable=3Dvariable) ? > can we just '{variable}.format()' ? > > if variable is exist, then assign it. > if variable is not exist, then raise error > > I am not language expert. so sorry if this is not a good idea, or already > discussed. You're asking for a facility whereby variables magically get pulled from the caller's scope. Let me put it to you another way: def func1(): return foo + " world" def func2(): foo =3D "Hello" print("func1 returns: "+func1()) func2() Will this work? If not, why not? There are ways around this; you can, for instance, pass locals() to format(= ): '{variable}'.format(**locals()) But this is massive overkill and can unexpectedly expose a whole lot more than you wanted (an issue if you accept a format string from an untrusted source). Generally, it's best to be explicit. You can avoid repeating the name so much by using positional parameters instead: '{0}'.format(variable) Of course, this has its own considerations. But at least it isn't magical. = :) ChrisA