List length operation "len(list)" should include (dot) operator format " list.len() "

list.append()
list.remove()
list.insert()
list.pop()
list.slice()
and so on
but when it comes to list length it’s len(list). We should have the option to write list.len()
thus making the language more uniform and orthogonal. and easier to code in some situations.

Adding yet one way of getting the length of the list would make the language less orthogonal.

Why do you need yet one way of getting length which will only work with lists if there is a standard way which works in all Python versions and with all containers?

4 Likes

Hi Andrew,
in other languages ​​such as Java and C#, each method in a class exactly matches the behavior specified in that method. This is what the OOP design pattern wants.
In Java, classes like Array and List may both have a length method that returns a different value despite the elements being identical. To change or extend its behavior, you should rewrite and overload the method.

In Python, the core designers of the language have decided to have a more functional approach, respecting the patterns of Functional programming. In python, the len function accepts a container object as the only input, such as a list. What actually happens is that the len function takes the object as input and literally calls its hook method __len __() and expects an integer return result. So see what happens:

>>> l = [1, 2, 3]
>>> len(l)
3
>>> l.__len__()
3

This type of approach is much more functional, because you just need to write a __len__ method to have support for the len function.
It is much more practical than Java, that you have to write boilerplate code for each (non-inherited) object that needs to have a length.

Also in python, it is better to use the countless hook methods that you recognize with the initial (method) and final dunder, to define behavior with respect to inventing methods. Another example would be the toString() of C#, Javascript, Java, etc …
In python, you just need a __str__ method in your class to be represented and called by str() or print(), which calls str() and in turn obj.__str__().

Here, too, the functional programming scheme saves you a mess of code.

I hope I have explained myself better and have made it clear.

3 Likes