What's the difference between the square bracket and dot notations in Python?

后端 未结 4 1874
悲&欢浪女
悲&欢浪女 2020-12-05 09:31

I come from a Javascript background (where properties can be accessed through both . and [] notation), so please forgive me, but what, exactly, is

4条回答
  •  佛祖请我去吃肉
    2020-12-05 10:20

    [] is the index to a container, such as a list or dictionary.

    . is the member of an object and modules. It can be a method, member data, or attribute.

    >>> xs = [1, 7, 3, 4, 5, 4, 3, 4, 1]
    
    >>> xs.count(4)
    3
    
    >>> xs[1]
    7
    

提交回复
热议问题