How can I get around declaring an unused variable in a for loop?

后端 未结 10 1228
自闭症患者
自闭症患者 2020-11-27 16:03

If I have a list comprehension (for example) like this:

[\'\' for x in myList]

Effectively making a new list that has an empty string for e

10条回答
  •  清酒与你
    2020-11-27 17:04

    No. As the Zen puts it: Special cases aren't special enough to break the rules. The special case being loops not using the items of the thing being iterated and the rule being that there's a "target" to unpack to.

    You can, however, use _ as variable name, which is usually understood as "intentionally unused" (even PyLint etc. knows and respect this).

提交回复
热议问题