def get_val
100
end
def testcode
s=<<EOS
This is a sample string that references a variable whose value is: #{get_val}
Incrementing the value: #{get_val + 1}
EOS
puts s
end
testcode
from templet import stringfunction
def get_val():
return 100
@stringfunction
def testcode(get_val):
"""
This is a sample string
that references a function whose value is: ${ get_val() }
Incrementing the value: ${ get_val() + 1 }
"""
print(testcode(get_val))
如果您需要的不仅仅是^{} 和^{} 提供的简单字符串格式,则可以使用^{} 模块插入Python表达式:
输出
^{pr2}$Python Templating with @stringfunction。在
作为一个C和Ruby程序员,我喜欢经典的
printf
式的方法:或者在有多个参数的情况下:
^{pr2}$我已经感觉到人们会因为这个而打我。然而,我发现这一点特别好,因为它在Ruby中的工作方式是一样的。在
使用格式方法:
按名称设置格式:
^{pr2}$相关问题 更多 >
编程相关推荐