1 minute read

Function len returns the number of items of an object, its length.

:gear: Python version: 3.14

The header of the function:

len(object, /)

Me, for example, I want to quickly check the length of a list or a pandas DataFrame to get number of rows, number of characters in a string, etc.

>>> len("25 characters long string")
>>> len(range(24))
>>> len([1, 2]*10)

This won’t work though:

>>> len(1)

giving us an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    len(1)
    ~~~^^^
TypeError: object of type 'int' has no len()

The argument to the len function has to be an object with the __len__ method implemented. len is just calling the object’s __len__ method with additional sanity checks, like that the returned value is int and <= sys.maxsize.

For example, I’m interested in defining the length of a centipede according to its number of legs:

class AlienCentipede:
  def __len__(self):
    return 100

Then I’m able to obtain its length by calling the len built-in on it:

>>> len(AlienCentipede())
100

The advantage of using len over calling the __len__ method directly (and in general this applies to all built-ins I believe) is the already mentioned sanity check. See the difference in the following example.

If the class would’ve been defined like this (for some (?) reason):

class AmILengthMeasurable:
  def __len__(self):
    return "my length is 10 meters, so I have to be!"

Then we would see the difference between the following two calls:

>>> AmILengthMeasurable().__len__()

"my length is 10 meters, so I have to be!"

>>> len(AmILengthMeasurable())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    len(AmILengthMeasurable())
    ~~~^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer

Question to the “crowd”: Why did I name the centipede AlienCentipede?

References

Comments