Re:PyFormat – Practical examples of old and new style string...
by fragmede on Wednesday April 15, 2015 @04:39PM
(#9383943)
They missed my favorite trick!
locals() will give you a dict of, well, local variables, which generally coincides with what you want, so:
works as expected.
[ Parent
| on HN ]
Re:PyFormat – Practical examples of old and new style string...
by reidrac on Thursday April 16, 2015 @04:30AM
(#9386598)
You can also:
a = 4
'A is: %(a)d' % locals()
Perhaps is my experience programming in C, but "format" looks lees familiar and I tend to use the old style formatting.
Re:PyFormat – Practical examples of old and new style string...
by quarktasche on Wednesday April 15, 2015 @06:06PM
(#9384521)
Might not work in some special cases though. Try:
def foo():
a = 4
def bar():
print("a = {a}".format(**locals()))
bar()
foo()
which will raise a KeyError, while it will work fine if you add a print(a)
to the end of bar().
Re:PyFormat – Practical examples of old and new style string...
by fragmede on Wednesday April 15, 2015 @06:16PM
(#9384588)
Actually, if you reference 'a' at all in bar, it will work:
def foo():
a = 4
def bar():
a
print("a = {a}".format(**locals()))
bar()
foo()
successfully prints a = 4
which is a bit confusing, but makes sense if you think about it.
Re:PyFormat – Practical examples of old and new style string...
by gcr on Wednesday April 15, 2015 @06:24PM
(#9384634)
In this case, 'a' isn't a local variable binding though.
Re:PyFormat – Practical examples of old and new style string...
by pyre on Thursday April 16, 2015 @02:01AM
(#9386110)
Right, but it's accessible in scope though. It's just a "gotcha" for that method that might be easy to overlook if you aren't paying attention (or if someone with less experience stumbles onto this 'trick' in production code).
Re:PyFormat – Practical examples of old and new style string...
by pfranz on Thursday April 16, 2015 @02:22AM
(#9386166)
I've seen this and tried to avoid it since getting bitten my moving code around and not catching which variables were used. It might have been even more obscured with something like TEMPLATED_STRING.format(locals())
Re:PyFormat – Practical examples of old and new style string...
by wodenokoto on Wednesday April 15, 2015 @10:34PM
(#9385598)
Why not just
a = 4
'A is:{}'.format(a)
Re:PyFormat – Practical examples of old and new style string...
by x1798DE on Thursday April 16, 2015 @08:35AM
(#9387473)
I think the point would be to do something like:
a = 3
b = 4
c = 6
"A is {a}, C is {c}, B is {b}".format(**locals())
Then you don't have to figure out specifying order or definitions like ".format(a=a, b=b, c=c)". I'd probably go the more explicit route in production code anyway, but for debugging / unstable development it seems useful.
Re:PyFormat – Practical examples of old and new style string...
by wodenokoto on Friday April 17, 2015 @04:10AM
(#9392833)
I'm still not convinced. In order to write the string portion of the code, I need to know the names of the variables in play and I have to order them in them in some order in the string regardless.
So I still don't think
"A is {a}, C is {c}, B is {b}".format(**locals())
is better than "A is {}, C is {}, B is {}".format(a, c, b)
Re:PyFormat – Practical examples of old and new style string...
by maxerickson on Wednesday April 15, 2015 @08:19PM
(#9385148)
str.format_map takes a mapping directly (added in 3.2).