3 ms·
isupper makes sense only for strings, thus it makes sense not to put it in the global namespace. len is available for strings, but also lists, dicts and many ot
by im3w1l 4y ago
isupper makes sense only for strings, thus it makes sense not to put it in the global namespace. len is available for strings, but also lists, dicts and many other things. It's so useful that a 3-letter name in the global namespace is justified. Is there a reason for not making it a method though? Yes, actually. By using the dunder system, it makes len special. And it avoids a situation where some objects use a.len() others use b.length() and yet others use c.size() or d.len, e.length, f.size. Python has officially blessed the len name over the others, and that's useful.
- z9znz 4y agoThat defense doesn't really hold up. len() makes no sense for numbers, booleans, None, and probably other types I cannot think of at the moment. Python has a LONG list of built in functions, most of which are only applicable to a few types of parameters. They could just as well be object methods. https://docs.python.org/3/library/functions.html https://docs.python.org/3/library/functions.html Some of those methods, like sorted(), are terrible names. A past tense verb name... so instead of telling Python, "Hey, please sort() this", you say "Please give me a sorted() thing". So you have the collection.sort() method or the built in sorted() method. They both sort, but they have different options; and significantly, sort() mutates the collection, whereas sorted() returns a new collection. This is inconsistent and creates unnecessary cognitive load. It's another example of a Frankenstein language.