为什么&#34 ;;"是否违反格式标准?

时间:2014-06-22 18:54:40

标签: python python-2.7

如您所知,;符号用于分隔这样的行:

print "Hai"; print "Bai"

从我所听到的情况来看,这违反了python的格式标准。那么,如果这违反格式标准,为什么要实现呢?它是如何正确使用的?

2 个答案:

答案 0 :(得分:6)

&#34 ;;"用于案件,你别无选择。例如。从命令行调用Python并愿意执行这两个语句时。

$ python -c "print 'Hai'; print 'Bai'"
Hai
Bai

或更复杂的案例:

$ python -c 'a=1; b=2; print "a + b = {a} + {b} = {res}".format(a=a, b=b, res=a+b)'
a + b = 1 + 2 = 3

注意:不要采取我的"没有其他选择"从字面上看,你会在评论中找到一些很好的替代方案。

答案 1 :(得分:2)

要延续Jan Vlcinsky的回答,如果你想快速确定某事物的效率,timeit会派上用场。例如,(from the docs):

python -m timeit -s 'text = "sample string"; char = "g"'  'char in text'