ChatGPT解决这个技术问题 Extra ChatGPT

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

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

arr.__len__()

If so, why the strange syntax?


S
Soviut
my_list = [1,2,3,4,5]
len(my_list)
# 5

The same works for tuples:

my_tuple = (1,2,3,4,5)
len(my_tuple)
# 5

And strings, which are really just arrays of characters:

my_string = 'hello world'
len(my_string)
# 11

It was intentionally done this way so that lists, tuples and other container types or iterables didn't all need to explicitly implement a public .length() method, instead you can just check the len() of anything that implements the 'magic' __len__() method.

Sure, this may seem redundant, but length checking implementations can vary considerably, even within the same language. It's not uncommon to see one collection type use a .length() method while another type uses a .length property, while yet another uses .count(). Having a language-level keyword unifies the entry point for all these types. So even objects you may not consider to be lists of elements could still be length-checked. This includes strings, queues, trees, etc.

The functional nature of len() also lends itself well to functional styles of programming.

lengths = map(len, list_of_containers)

len() is a language command, __len__() is a method on container types.
len() is a global, builtin function; __len__() is a method that object can implement. len(foo) usually ends up calling foo.__len__().
You mention that by supplying len() each container does not have to implement a .length() method, but how is this different, if each type still implements a __len__() method which gets called by len() anyways? Is different container types handled differently by len()?
@Simon: the bit about "don't all need to implement .length()" is confusing. Container types still need to implement a method for returning their length; the point is that it's a standardized protocol, not an ad-hoc method you have to look up for each type. The double-underscores signify this.
I agree with Carl Meyer - saying that doesn't "need to explicitly implement" a public .length() method is misleading and at its core-meaning, incorrect. Anything will still have to implement len, and can always simply implement their own length method named whatever they want - circumventing the len function. So really I see this as some arbitrary weirdness that fits with how Guido sees the world. It probably has nothing to do with any universal reasoning.
n
nosklo

The way you take a length of anything for which that makes sense (a list, dictionary, tuple, string, ...) is to call len on it.

l = [1,2,3,4]
s = 'abcde'
len(l) #returns 4
len(s) #returns 5

The reason for the "strange" syntax is that internally python translates len(object) into object.__len__(). This applies to any object. So, if you are defining some class and it makes sense for it to have a length, just define a __len__() method on it and then one can call len on those instances.


A
Ayxan Haqverdili

Just use len(arr):

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

U
UncleZeiv

Python uses duck typing: it doesn't care about what an object is, as long as it has the appropriate interface for the situation at hand. When you call the built-in function len() on an object, you are actually calling its internal __len__ method. A custom object can implement this interface and len() will return the answer, even if the object is not conceptually a sequence.

For a complete list of interfaces, have a look here: http://docs.python.org/reference/datamodel.html#basic-customization


D
David Locke

The preferred way to get the length of any python object is to pass it as an argument to the len function. Internally, python will then try to call the special __len__ method of the object that was passed.


A
Ahmed Abobakr

you can use len(arr) as suggested in previous answers to get the length of the array. In case you want the dimensions of a 2D array you could use arr.shape returns height and width


H
Harun ERGUL

len(list_name) function takes list as a parameter and it calls list's __len__() function.


J
JOHNKYON

Python suggests users use len() instead of __len__() for consistency, just like other guys said. However, There're some other benefits:

For some built-in types like list, str, bytearray and so on, the Cython implementation of len() takes a shortcut. It directly returns the ob_size in a C structure, which is faster than calling __len__().

If you are interested in such details, you could read the book called "Fluent Python" by Luciano Ramalho. There're many interesting details in it, and may help you understand Python more deeply.