When I assign a list to variable why Pycharm give me a prompt that is “this list creation could be rewritten as a list literal”?

前端 未结 2 1717
滥情空心
滥情空心 2021-02-01 01:04

I am a Python beginner and have a puzzle. When I write code like this:

lst = [1, 2, 3, 4]

Pycharm give me a prompt that is \"this list creation

2条回答
  •  自闭症患者
    2021-02-01 01:51

    It's totally legal to write code like that in Python. However, writing code like

    lst = [1, 2, 3, 4, 12]
    

    would be "better" than

    lst = [1, 2, 3, 4]
    ... # code has nothing do to with lst
    lst.append(12)
    

    In general, the former one would have better performance than the latter one, but if the latter one is more readable in your case/you have a good reason doing that, then you can ignore the PyCharm prompt.

    If it bothers you, you can turn this inspection off in

    "PyCharm->settings->editor->inspection->Python->List creation could be..."

提交回复
热议问题