Why len python




















Viewed k times. Update Ok, I realized I was embarrassingly mistaken. Improve this question. Add a comment. Active Oldest Votes. Improve this answer. Jonny Buchanan Jonny Buchanan It astonishes me how moronic the reason for using len is. They think that it is easier to force people to implement. Its the same thing, and one looks much cleaner.

Not everything revolves around OOP, even in Python. Also, by using a protocol, they can provide alternative ways of implementing things. And so on. I think Armin explains this reasonably well in the linked post—but even if he didn't, calling Python moronic because of an outside explanation by a guy who's often publicly at odds with the core devs wouldn't exactly be fair… — abarnert.

Python unlike Java, Ruby, or Smalltalk doesn't try to be a "pure OOP" language; it's explicitly designed to be a "multi-paradigm" language. List comprehensions aren't methods on iterables. Just because everything is an object doesn't mean being an object is the most important thing about each thing. That article is so poorly written I feel confused and angry after trying to read it.

Quoting Guido van Rossum: First of all, I chose len x over x. There are two intertwined reasons actually, both HCI: a For some operations, prefix notation just reads better than postfix — prefix and infix!

Federico A. Ramponi Federico A. Ramponi 44k 29 29 gold badges silver badges bronze badges. Yes, but it's not meant to be used directly. Dunder methods in general are not meant to be called directly. Luciano Ramalho Luciano Ramalho 1, 18 18 silver badges 21 21 bronze badges. I do not buy the optimality argument. You have instance attributes, class attributes, data descriptors, not-data descriptors, and multiple-inheritance. All that may play a role in a call like s. Alex Coventry Alex Coventry The short answer is that Guido liked it this way.

The main reason that len works so well and has remained part of the Python language is that it provides a consistent surface interface to check the number of things an object contains. Because len is part of the language, we know what to expect from its input and output. This simple indirection leads to a deeper consistency: all containers implement the same method to answer this question. 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 Empty is a second return call which has zero characters, but it is always None. Len function depends on the type of the variable passed to it. A Non-Type does not have any built-in support.

For the dictionary, each pair is counted as one unit. However, values and keys are not independent. Skip to content. Report a Bug. Previous Prev.



0コメント

  • 1000 / 1000