0
例如:什么是最简洁的方式将变量传递给python string.format?
def print_template(a,b):
c="the c"
template="{a} {b} {c}"
print template.format(a=a,b=b,c=c)
这是工作,但格式函数的参数列表是一个有点冗长。无论如何要让它更简洁?也许通过传递所有的局部变量到格式函数?
例如:什么是最简洁的方式将变量传递给python string.format?
def print_template(a,b):
c="the c"
template="{a} {b} {c}"
print template.format(a=a,b=b,c=c)
这是工作,但格式函数的参数列表是一个有点冗长。无论如何要让它更简洁?也许通过传递所有的局部变量到格式函数?
您可以通过局部变量,如果你想:
def print_template(a,b):
c="the c"
template="{a} {b} {c}"
print template.format(**vars())
演示:
>>> print_template(1,2)
1 2 the c
谢谢,alko,它的效果很好 – yigal
局部变量可以包括其他对象,功能和内容。你确定要打印所有当地人? – aIKid