我相当擅长python,但是我想知道是否以任何方式都忽视了使用半冒号。当我在网上看到代码片段时,或者在查看朋友代码时,我看不到它们。如果他们被轻视,为什么?我认为他们没有问题。我会像这样使用它们:
print("What you do want {}?".format(name));choice = input(">> ")
答案 0 :(得分:2)
通常不建议这样做,因为它会降低可读性,原因是PEP8:
复合语句(同一行上有多个语句)是 通常不鼓励。
是:
if foo == 'blah': do_blah_thing() do_one() do_two() do_three()
而不是:
if foo == 'blah': do_blah_thing() do_one(); do_two(); do_three()
虽然有时可以将if / for / while放在很小的身体上 在同一行中,对于多子句语句永远不要这样做。也要避免 折叠这么长的线!
而不是:
if foo == 'blah': do_blah_thing() for x in lst: total += x while t < 10: t = delay()
绝对不是:
if foo == 'blah': do_blah_thing() else: do_non_blah_thing() try: something() finally: cleanup() do_one(); do_two(); do_three(long, argument, list, like, this) if foo == 'blah': one(); two(); three()