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.
On Fri, Mar 1, 2013 at 11: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.
Explicit is better than implicit.
There are also security issues with automatically making all local variables available to a format string, where that format string might come from an untrusted source.
If you want the behavior you suggest, you can use '{variable}'.format(**locals()) unless you don't intend to include global variables. I don't recall the behavior of locals() with regard to non-global variables from enclosing scopes.
Dustin
On Fri, Mar 1, 2013, at 11:55, 김용빈 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.
_______________________________________________
Python-ideas mailing list
[1]Python-ideas@python.org
[2]http://mail.python.org/mailman/listinfo/python-ideas
If you don't want to repeat a name multiple times, just use '{0}'.format(variable)
The format function doesn't (i think?) have a way to see your local variables to look up the name.
References
1. mailto:Python-ideas@python.org 2. http://mail.python.org/mailman/listinfo/python-ideas
On Fri, Mar 1, 2013 at 11:55 AM, 김용빈 kybinz@gmail.com wrote:
why we bother with '{variable}'.format(variable=variable) ? can we just '{variable}.format()' ?
variable = "Hello, World!" print('{variable}'.format_map(locals()))
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.
As Dustin said, Explicit is Better Than Implicit; you don't want a variable to accidentally make your string formatting work by luck because you forgot to pass in an argument you meant to but just so happened to have a variable with the "right" name.
A friend of mine wrote a library which does this: https://pypi.python.org/pypi/ScopeFormatter
It's super handy when doing scripting/debugging.
On Fri, Mar 1, 2013 at 1:51 PM, Brett Cannon brett@python.org wrote:
On Fri, Mar 1, 2013 at 11:55 AM, 김용빈 kybinz@gmail.com wrote:
why we bother with '{variable}'.format(variable=variable) ? can we just '{variable}.format()' ?
variable = "Hello, World!" print('{variable}'.format_map(locals()))
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.
As Dustin said, Explicit is Better Than Implicit; you don't want a variable to accidentally make your string formatting work by luck because you forgot to pass in an argument you meant to but just so happened to have a variable with the "right" name.
Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas