Intuitively a List
class should implement an attribute or a method to retrieve the length of the instance. Fortunately Python'lists have a hidden method called __len__
. Unfortunately this method is not meant to be used directly. I should instead use an external function that will read the hidden method for me.
It is like I need to ask someone else to open the fridge to grab a beer for me. The beer is in the fridge, I have ma both hands and I should be able to do it myself.
Conceptually this approach seems curious. Why not having an attribute, (rather than a method) for getting the length of a list.
In other words, I would prefer using foo.len
instead of foo.len()
or foo.__len__
. len(foo)
appears more bizarre to me.
Is there an explanation for this implementation?
This answer answers partially my question, but my frustration remains.