In Fluent Python they say
You write
len(my_object)and, ifmy_objectis an instance of a user-defined class, then Python calls the__len__instance method you implemented ...But for built-in types like
list,str,bytearray, and so on, the interpreter takes a shortcut: the CPython implementation oflen()actually returns the value of theob_sizefield in thePyVarObjectC struct that represent the any variable-sized built-in object in memory.
But __len__ is still defined on at least some of these ...
>>> 'string'.__len__()
6
>>> [1, 2, 3].__len__()
3
What purpose do these methods serve if they're not called by len? And if are there other similar examples, what about them?