Some list operations are reached by a dot notation
C.append(e)
while other operations requires the list object as an argument to a function, as in
len(C)
Why this happens? Are there any rules whether functionality regarding an object is reached through the first way (method) or the second one (function)? Thanks.
1 Answer 1
There is no difference for many built-in functions. The global functions like len, iter, str are calls to object methods __len__, __iter__, __str__ etc.
See Basic customization and Emulating container types in Python reference:
object.
__len__(self)Called to implement the built-in function len(). Should return the length of the object, an integer>= 0. ...
Advantage of such soluttion is that an object can override these special functions just by overriding corresponding "double-underscore" methods, e.g __len__.
Although append does not have a built-in, since it is a less universal operation than len, str etc.
lenis a global builtin.len, I believe it just calls the object's__len__method anyway. When in doubt, consult the documentation.