Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我认为可以逃脱的简单的事情:
foo = True print('bar') if foo else pass
产生:
SyntaxError: invalid syntax
当然,我可以替换pass为None,它会起作用的。我只是好奇:为什么不通过工作?
pass
None
pass是语句而不是表达式。
表达式几乎可以在任何地方使用。
大多数语句都有其特殊的语法,通常在自己的一行中。
有关两者之间差异的更多信息,请参阅此答案。
您可以在一行中执行此操作,因为 else 什么都不做,不需要 else 块。
foo = True if foo : print('bar')