October 2018
Beginner to intermediate
466 pages
12h 2m
English
The simplest example is the len() function, which counts the number of items in some kind of container object, such as a dictionary or list. You've seen it before, demonstrated as follows::
>>> len([1,2,3,4]) 4
You may wonder why these objects don't have a length property instead of having to call a function on them. Technically, they do. Most objects that len() will apply to have a method called __len__() that returns the same value. So len(myobj) seems to call myobj.__len__().
Why should we use the len() function instead of the __len__ method? Obviously, __len__ is a special double-underscore method, suggesting that we shouldn't call it directly. There must be an explanation for this. The Python developers don't make ...