3 ms·
The Python design FAQ explains why Len is a built-in function instead of a method. Guido wanted a guarantee that whenever he called it, it'd return a whole numb
by xapata 9y ago
The Python design FAQ explains why Len is a built-in function instead of a method. Guido wanted a guarantee that whenever he called it, it'd return a whole number. As just another method, you might find a new type that defines a length in fractional terms, which would break any code wanting to use that as a list index, etc.
- goatlover 9y agoBut that would be on your for misusing the length method. All the built-in and popular libraries would still define length properly.
- xapata 9y agoPerhaps, but "your" mistake might impede the popularity of the language. A wide range of well-designed community libraries are a major reason to choose Python. Take a look at the design rationale for the new __matmul__ magic method. It's not for the standard library, it's for the community.
- antonvs 9y agoThe need to do that reveals a couple of weaknesses (or more charitably, tradeoffs) of Python: lack of type constraints, and (related) lack of well-defined, invariant interfaces on objects.
- xapata 9y agoTrade-offs indeed. Magic methods indicate the use of interfaces defined by traits rather than formal types. Duck Typing. It's flexible. But you can also see that Guido has been into types lately. He's been pushing the development of mypy and the new optional type system.