Python why isn len a method




















Getting the number of items in a collection is a common operation and must work efficiently for such basic and diverse types as str , list , array. However, to promote consistency, when applying len o to a user-defined type, Python calls o.

By implementing special methods your objects can support iteration, overload infix operators, manage contexts in with blocks etc. You can think of the Data Model as a way of using the Python language itself as a framework where the objects you create can be integrated seamlessly.

A second reason, supported by quotes from Guido van Rossum like this one , is that it is easier to read and write len s than s. The notation len s is consistent with unary operators with prefix notation, like abs n. There may also be a historical reason: in the ABC language which preceded Python and was very influential in its design , there was a unary operator written as s which meant len s.

There are some great answers here, and so before I give my own I'd like to highlight a few of the gems no ruby pun intended I've read here. If you don't like the way this works in your own code, it's trivial for you to re-implement the containers using your preferred method see example below.

The fact that len is a function means that classes cannot override this behaviour to avoid the check. As such, len obj gives a level of safety that obj. Of course, it is possible to "override" the len function by reassigning it as a global variable, but code which does this is much more obviously suspicious than code which overrides a method in a class. Why does Python code use len function instead of a length method?

Update Ok, I realized I was embarrassingly mistaken. Asked 3 years, 2 months ago. Active 3 years, 2 months ago. Viewed 1k times. Why does Python code use len function on strings instead of a string length method? How to get the number of elements in a list in Python? Can't see why you would ever do this, other than for testing. What is the meaning of a single- and a double-underscore before an object name?

In my opinion, given your expertise, I think it would be a good idea to self-answer this question, i. The only correct answer to these sorts of "why" questions is " Because that's how Guido chose to do it ," which IMHO is not terribly enlightening.

It's little more than a personal opinion about "what's more readable. Kevin: that's not what I'm asking here. Like most Python programmers, I already know the answers, per the links I cited. I'm merely saying there are multiple distinct questions, and we must pick multiple canonicals for SO. No they are not off-topic, because Python programmers need to first learn these basics, before understanding how to create their own classes. Instead of giving us the number of characters in the string, count takes a string as an argument and counts occurrences of the input string in the target string.

Have I argued the point enough? A protocol in Python is an informal interface. User-defined classes that implement all of the methods of a protocol can hook into system behavior through built-in functions like range and syntax like for I have enough to say about protocols — and especially Python 3. A Story February A question has bothered me for the past year or more: is FastAPI a fad, or sh Andrew Brookins — 28 June



0コメント

  • 1000 / 1000