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

Rate this post


Summary: The preferred approach to find the length of any given Python object is to use the len function and pass the object within the function as an argument. Python will then implicitly call the special __len__ method on the object that was passed.

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

Array is not a built-in data type in Python. We have to use lists instead of arrays in Python. We can also create arrays in Python by using third-party libraries like Numpy.

Python provides us with two ways to find the length of an array-

  • The len() method
    • len is basically a function that is used to get the length of a collection. It calls an object’s __len__ method to operate. __something__ are special attributes and have more than meets the eye. You should avoid using them directly unless absolutely necessary.
  • size attribute of the Numoy module.

Having said that, let us dive into our mission-critical question and learn the different ways to calculate the length of an array in Python and also discover the most suitable approach to do so.

❒Using len() Function To Get The Length Of The Array

len() is a built-in function in Python. The function returns the total number of elements in an iterable object by taking lists, tuples, arrays, or strings as an argument. In our case, it will return the size of the array.

Related Video :

Python len() -- A Simple Guide


# Using len to get the length of an array
# Given array
a = ["orange", "mango", "pear", "apple", "kiwi"]
print("The given array", a)
b = "5678910"
print("The given string", b)
# Calculating the length of the array
s = len(a)
print("The length of the given array is", s)
# Calculating the length of string
l = len(b)
print("The length of the given string is", l)


The given array ['orange', 'mango', 'pear', 'apple', 'kiwi']
The given string 5678910
The length of the given array is 5
The length of the given string is 7

➀ Note: The length of an array is adding one to the highest index of the array. For example, in an array, if the index of the last element is 3, the length of an array will be 4. 

Recommended Read: Accessing The Index Of Iterables In Python

❒Using __len__() To Get The Length Of The Array

  • When we calculate the length of any object in Python by calling the len() function on it, len() implicitly calls the __len__() function on the passed object to calculate its length. This means that Python will internally translate the len(object) into an object.__len__() as Python utilizes duck typing, i.e. Python does not care about what type an object is till it has the proper interface for the situation at hand. Hence, when we call the built-in method- len() on an object, it is actually calling its internal __len__ method. Now any object can implement this interface, and the method len() will return the answer, even if the object is not conceptually a sequence. Hence, instead of using   __len__() on an object in Python, it is preferred to call the len() function on the object.
    • Thus the bottom line is – it is suggested to use the len() method instead of using the __len__() to maintain consistency in your code.
  • One more advantage of using the len()  method over using the __len__() method is no exceptions.  When we pass the object to the len() method, it is assured that no exceptions are being raised during the execution of the call. Hence, the program will continue to run even after the call. This certainty that we have with the len() method is also referred to as sanity checks in Python. However, there is no such assurance when we are using the __len__() method. It is uncertain whether the program will throw or not throw any exceptions when the function call is running.  Therefore, it is preferable to use the len() method over the __len__() method when coming to sanity checks of the program or code.
  • One more thing that we can agree on is that the len() method is more readable to call on an object. However, the same is not true when we talk about calling the __len__() method directly.

❒Calculating The Array Length Using Numpy

We can calculate the size of the array by using the size attribute of the Numpy module. The Numpy module in Python facilitates us with various size and shape attributes. size is a one such built-in attribute from the Numpy module that is used to return the size of an array. We can even calculate the shape of the array using the shape function from the Numpy module.


# Importing the numpy module
import numpy as np
# Given array
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("The given array", a)
# Calculating the length
s = a.size
print("The length of the given array is", s)
# Calculating the shape of the numpy array
x = a.shape
print("The shape of the given array is: ", x)


The given array [[1 2 3]
 [4 5 6]
 [7 8 9]]
The length of the given array is 9
The shape of the given array is:  (3, 3)


Thus we have unearthed the answer to our question and it is evident from the discussion that using len() method and passing the object as an argument to the len method is the preferred approach rather than using arr.__len__(). I hope this discussion helped you and answered your query. Please stay tuned and subscribe for more interesting discussions.

Authors: Shubham Sayon and Rashi Agarwal

To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members: