Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40274
| References | <CAK-Ep0o=gtsCjhhAzYDd_NfaVJY-LLzsC2YQxj2f3CL8m62zmA@mail.gmail.com> |
|---|---|
| Date | 2013-03-02 04:07 +1100 |
| Subject | Re: [Python-ideas] string.format() default variable assignment |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2741.1362157649.2939.python-list@python.org> (permalink) |
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