Is arr.__len__() the preferred way to get the length of an array in Python?

前端 未结 8 1020
悲哀的现实
悲哀的现实 2020-12-04 05:18

In Python, is the following the only way to get the number of elements?

arr.__len__()

If so, why the strange syntax?

8条回答
  •  孤城傲影
    2020-12-04 05:23

    Just use len(arr):

    >>> import array
    >>> arr = array.array('i')
    >>> arr.append('2')
    >>> arr.__len__()
    1
    >>> len(arr)
    1
    

提交回复
热议问题