by fragmede · 11y ago · 0 comments
a = 4
'A is: {a}'.format(**locals())
works as expected. 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. 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(). 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. a = 4
'A is:{}'.format(a) 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.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)