Python: What does the use of [] mean here?

后端 未结 4 1189
谎友^
谎友^ 2021-01-18 19:21

What is the difference in these two statements in python?

var = foo.bar

and

var = [foo.bar]

I think it i

4条回答
  •  醉话见心
    2021-01-18 19:57

    I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

    • Yes, it creates a new list.

    • If foo.bar is already a list, it will simply become a list, containing one list.

      h[1] >>> l = [1, 2]
      h[1] >>> [l]
      [[1, 2]]
      h[3] >>> l[l][0]
      [1, 2]
      

提交回复
热议问题