Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40274
| 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 | <rosuav@gmail.com> |
| 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 | <CAK-Ep0o=gtsCjhhAzYDd_NfaVJY-LLzsC2YQxj2f3CL8m62zmA@mail.gmail.com> |
| References | <CAK-Ep0o=gtsCjhhAzYDd_NfaVJY-LLzsC2YQxj2f3CL8m62zmA@mail.gmail.com> |
| Date | Sat, 2 Mar 2013 04:07:19 +1100 |
| Subject | Re: [Python-ideas] string.format() default variable assignment |
| From | Chris Angelico <rosuav@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2741.1362157649.2939.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Sat, Mar 2, 2013 at 3:55 AM, 김용빈 <kybinz@gmail.com> wrote:
> why we bother with '{variable}'.format(variable=variable) ?
> 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 = "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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-02 04:07 +1100
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-02 01:43 +0000
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-02 14:54 +1100
Re: [Python-ideas] string.format() default variable assignment Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-03-02 01:09 -0500
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-02 17:54 +1100
Re: [Python-ideas] string.format() default variable assignment James Griffin <jmz.griffin@kode5.net> - 2013-03-02 08:06 +0000
Re: [Python-ideas] string.format() default variable assignment Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-03-02 09:53 -0500
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-03 00:27 +0000
Re: [Python-ideas] string.format() default variable assignment Roy Smith <roy@panix.com> - 2013-03-02 19:42 -0500
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-03 03:19 +0000
Re: [Python-ideas] string.format() default variable assignment David Robinow <drobinow@gmail.com> - 2013-03-02 21:11 -0500
Re: [Python-ideas] string.format() default variable assignment Roy Smith <roy@panix.com> - 2013-03-02 21:15 -0500
Re: [Python-ideas] string.format() default variable assignment David Robinow <drobinow@gmail.com> - 2013-03-02 22:01 -0500
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-03 14:06 +1100
Re: [Python-ideas] string.format() default variable assignment David Robinow <drobinow@gmail.com> - 2013-03-02 22:18 -0500
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-03 14:46 +1100
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-03 03:21 +0000
Re: [Python-ideas] string.format() default variable assignment Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-03-02 23:00 -0500
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-03 10:33 +0000
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-03 09:51 +0000
Re: [Python-ideas] string.format() default variable assignment David Robinow <drobinow@gmail.com> - 2013-03-04 11:09 -0500
Re: [Python-ideas] string.format() default variable assignment Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-05 02:42 +0000
Re: [Python-ideas] string.format() default variable assignment Chris Angelico <rosuav@gmail.com> - 2013-03-05 13:54 +1100
Re: [Python-ideas] string.format() default variable assignment Gene Heskett <gheskett@wdtv.com> - 2013-03-04 23:01 -0500
Re: [Python-ideas] string.format() default variable assignment James Griffin <jmz@kontrol.kode5.net> - 2013-03-05 08:16 +0000
Re: [Python-ideas] string.format() default variable assignment Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-05 11:35 +0000
Re: [Python-ideas] string.format() default variable assignment David Robinow <drobinow@gmail.com> - 2013-03-04 11:15 -0500
Re: [Python-ideas] string.format() default variable assignment Dave Angel <davea@davea.name> - 2013-03-04 13:31 -0500
Re: [Python-ideas] string.format() default variable assignment Tim Golden <mail@timgolden.me.uk> - 2013-03-02 15:01 +0000
Re: [Python-ideas] string.format() default variable assignment Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-03-02 12:31 -0500
good mail readers [was Re: [Python-ideas] string.format() default variable assignment] Ethan Furman <ethan@stoneleaf.us> - 2013-03-02 11:59 -0800
csiph-web