gpt4 book ai didi

python - "else"在 Python 中被认为是有害的?

转载 作者:太空狗 更新时间:2023-10-29 17:51:42 24 4
gpt4 key购买 nike

answer 中(由 S.Lott) 回答关于 Python 的 try...else 语句的问题:

Actually, even on an if-statement, the else: can be abused in truly terrible ways creating bugs that are very hard to find. [...]

Think twice about else:. It is generally a problem. Avoid it except in an if-statement and even then consider documenting the else- condition to make it explicit.

这是一个广泛持有的观点吗?是 else considered harmful ?

当然,您可以用它编写令人困惑的代码,但任何其他语言结构都是如此。即使是 Python 的 for...else 在我看来也是一件非常方便的事情(try...else 就不太方便了)。

最佳答案

S.Lott 显然看到了一些糟糕的代码。我们不是吗?我不认为 else 有害,尽管我已经看到它用来编写错误的代码。在那些情况下,所有周围的代码也很糟糕,那么为什么要责怪其他人呢?

关于python - "else"在 Python 中被认为是有害的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/865741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com