mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	bpo-27671: Update FAQ about why len is function (GH-8432)
This commit is contained in:
		
							parent
							
								
									b229b072a9
								
							
						
					
					
						commit
						c48e26dcad
					
				
					 1 changed files with 16 additions and 15 deletions
				
			
		| 
						 | 
					@ -215,24 +215,25 @@ objects using the ``for`` statement.  For example, :term:`file objects
 | 
				
			||||||
Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
 | 
					Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
 | 
				
			||||||
----------------------------------------------------------------------------------------------------------------
 | 
					----------------------------------------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The major reason is history. Functions were used for those operations that were
 | 
					As Guido said:
 | 
				
			||||||
generic for a group of types and which were intended to work even for objects
 | 
					 | 
				
			||||||
that didn't have methods at all (e.g. tuples).  It is also convenient to have a
 | 
					 | 
				
			||||||
function that can readily be applied to an amorphous collection of objects when
 | 
					 | 
				
			||||||
you use the functional features of Python (``map()``, ``zip()`` et al).
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
In fact, implementing ``len()``, ``max()``, ``min()`` as a built-in function is
 | 
					    (a) For some operations, prefix notation just reads better than
 | 
				
			||||||
actually less code than implementing them as methods for each type.  One can
 | 
					    postfix -- prefix (and infix!) operations have a long tradition in
 | 
				
			||||||
quibble about individual cases but it's a part of Python, and it's too late to
 | 
					    mathematics which likes notations where the visuals help the
 | 
				
			||||||
make such fundamental changes now. The functions have to remain to avoid massive
 | 
					    mathematician thinking about a problem. Compare the easy with which we
 | 
				
			||||||
code breakage.
 | 
					    rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
 | 
				
			||||||
 | 
					    doing the same thing using a raw OO notation.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. XXX talk about protocols?
 | 
					    (b) When I read code that says len(x) I *know* that it is asking for
 | 
				
			||||||
 | 
					    the length of something. This tells me two things: the result is an
 | 
				
			||||||
 | 
					    integer, and the argument is some kind of container. To the contrary,
 | 
				
			||||||
 | 
					    when I read x.len(), I have to already know that x is some kind of
 | 
				
			||||||
 | 
					    container implementing an interface or inheriting from a class that
 | 
				
			||||||
 | 
					    has a standard len(). Witness the confusion we occasionally have when
 | 
				
			||||||
 | 
					    a class that is not implementing a mapping has a get() or keys()
 | 
				
			||||||
 | 
					    method, or something that isn't a file has a write() method.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. note::
 | 
					    -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
 | 
				
			||||||
 | 
					 | 
				
			||||||
   For string operations, Python has moved from external functions (the
 | 
					 | 
				
			||||||
   ``string`` module) to methods.  However, ``len()`` is still a function.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Why is join() a string method instead of a list or tuple method?
 | 
					Why is join() a string method instead of a list or tuple method?
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue