在 .format() 和 **locals() 中使用它之前是否可以在不创建新变量的情况下操作局部变量?所以与此具有相同效果的东西:
medium="image"
size_name="double"
width=200
height=100
width2=width*2
height2=height*2
print "A {size_name} sized {medium} is {width2} by {height2}".format(**locals())
但更优雅,无需创建 width2 和 height2 变量。我试过这个:
medium="image"
size_name="double"
width=200
height=100
print "A {size_name} sized {medium} is {width2} by {height2}".format(**locals(),height2=height*2,width2=width*2)
但它会在 locals() 之后的第一个逗号处引发错误“SyntaxError: invalid syntax”。