Is there any difference between calling len([1,2,3])
or [1,2,3].__len__()
?
If there is no visible difference, what is done differently behind the scenes?
Is there any difference between calling len([1,2,3])
or [1,2,3].__len__()
?
If there is no visible difference, what is done differently behind the scenes?
len
is a function to get the length of a collection. It works by calling an object's __len__
method. __something__
attributes are special and usually more than meets the eye, and generally should not be called directly.
It was decided at some point long ago getting the length of something should be a function and not a method code, reasoning that len(a)
's meaning would be clear to beginners but a.len()
would not be as clear. When Python started __len__
didn't even exist and len
was a special thing that worked with a few types of objects. Whether or not the situation this leaves us makes total sense, it's here to stay.
It's often the case that the "typical" behavior of a built-in or operator is to call (with different and nicer syntax) suitable magic methods (ones with names like __whatever__
) on the objects involved. Often the built-in or operator has "added value" (it's able to take different paths depending on the objects involved) -- in the case of len
vs __len__
, it's just a bit of sanity checking on the built-in that is missing from the magic method:
>>> class bah(object):
... def __len__(self): return "an inch"
...
>>> bah().__len__()
'an inch'
>>> len(bah())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
When you see a call to the len
built-in, you're sure that, if the program continues after that rather than raising an exception, the call has returned an integer, non-negative, and <= sys.maxsize
-- when you see a call to xxx.__len__()
, you have no certainty (except that the code's author is either unfamiliar with Python or up to no good;-).
Other built-ins provide even more added value beyond simple sanity checks and readability. By uniformly designing all of Python to work via calls to builtins and use of operators, never through calls to magic methods, programmers are spared from the burden of remembering which case is which. (Sometimes an error slips in: until 2.5, you had to call foo.next()
-- in 2.6, while that still works for backwards compatibility, you should call next(foo)
, and in 3.*
, the magic method is correctly named __next__
instead of the "oops-ey" next
!-).
So the general rule should be to never call a magic method directly (but always indirectly through a built-in) unless you know exactly why you need to do that (e.g., when you're overriding such a method in a subclass, if the subclass needs to defer to the superclass that must be done through explicit call to the magic method).
def len(x): return "I am a string." print(len(42)) print(len([1,2,3]))
and it printed I am string
twice. Can you explain it more? –
Giddens __len__
special method (not function) on the object under consideration. –
Ocher len
not some other function (like in my example) that happened to have same name - len
. There is no warning like "You are redefining built-in function len" or something like this. In my opinion, I cannot be sure about what Alex stated in his answer. –
Giddens len in vars(__builtins__).values()
. –
Ocher You can think of len() as being roughly equivalent to
def len(x):
return x.__len__()
One advantage is that it allows you to write things like
somelist = [[1], [2, 3], [4, 5, 6]]
map(len, somelist)
instead of
map(list.__len__, somelist)
or
map(operator.methodcaller('__len__'), somelist)
There is slightly different behaviour though. For example in the case of ints
>>> (1).__len__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__len__'
>>> len(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
operator.methodcaller
instead of operator.attrgetter
. –
Mathematician You can check Pythond docs:
>>> class Meta(type):
... def __getattribute__(*args):
... print "Metaclass getattribute invoked"
... return type.__getattribute__(*args)
...
>>> class C(object):
... __metaclass__ = Meta
... def __len__(self):
... return 10
... def __getattribute__(*args):
... print "Class getattribute invoked"
... return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__() # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) # Implicit lookup
10
Well, len(s)
is a built-in Python method which returns the length of an object. Now __len__()
is a special method that is internally called by len(s)
method to return the length of an object.
So, when we call len(s)
method, s.__len__()
is what actually happening behind the scenes to calculate the length.
The Python len()
function can be interpreted as:
def len(s):
return s.__len__()
One additional comment:
len(c)
and c.__len__()
can return different values. This situation generally happens when we modify the __len__
function of the instance c
.
>>> class C:
... def __len__(self):
... return 10
...
>>> c = C()
>>> c.__len__ = lambda: 3
>>> len(c) # C's __len__ invoked
10
>>> type(c).__len__(c) # C's __len__ invoked
10
>>> c.__len__() # c's __len__ invoked
3
For built-in types, let's take list
for instance. If you use help(list.__len__)
to look up how list.__len__
works, it says:
>>> help(list.__len__)
Help on wrapper_descriptor:
__len__(self, /)
Return len(self).
This indicates that for built-in data types, unlike the anwsers above, data_type.__len__()
will call len(data_type)
, instead of the case that the latter calls the former.
Actually, if x is an instance of a built-in type, when you call
len(x)
CPython will read the length of the object directly from a C structure without calling any methods at all. Obtaining the number of elements in a collection is a very common operation. On types such asstr
,list
, andmemoryview
, this operation must be efficient.
From Fluent Python, Luciano Ramalho
Please correct my words if I got them wrong.
© 2022 - 2024 — McMap. All rights reserved.