[Python-ideas] Make map() better

MRAB python at mrabarnett.plus.com
Thu Sep 14 12:43:24 EDT 2017


On 2017-09-14 03:55, Steven D'Aprano wrote:
> 
> On Wed, Sep 13, 2017 at 11:05:26PM +0200, Jason H wrote:
> 
>> > And look, map() even works with all of them, without inheritance,
>> > registration, and whatnot. It's so easy!
>> 
>> Define easy. 
> 
> Opposite of hard or difficult.
> 
> You want to map a function?
> 
> map(function, values)
> 
> is all it takes. You don't have to care whether the collection of values
> supports the map() method, or whether the class calls it "apply", or
> "Map", or something else. All you need care about is that the individual
> items inside the iterable are valid for the function, but you would need
> to do that regardless of how you call it.
> 
> [1, 2, 3, {}, 5].map(plusone)  # will fail
> 
> 
>> It's far easier for me to do a dir(dict) and see what I can do with it.
> 
> And what of the functions that dict doesn't know about?
> 
> 
> 
>>  This is what python does after all. "Does it have the interface I 
>> expect?" Global functions like len(), min(), max(), map(), etc(), 
>> don't really tell me the full story. len(7) makes no sense. I can 
>> attempt to call a function with an invalid argument.
> 
> And you can attempt to call a non-existent method:
> 
> x = 7
> x.len()
> 
> Or should that be length() or size() or count() or what?
> 
>> [].len() makes more sense.
> 
> Why? Getting the length of a sequence or iterator is not specifically a
> list operation, it is a generic operation that can apply to many
> different kinds of things.
> 
> 
>> Python is weird in that there are these special magical 
>> globals 
> 
> The word you want is "function".
> 
>> that operate on many things.
> 
> What makes that weird? Even Javascript has functions. So do C, Pascal,
> Haskell, C++, Lisp, Scheme, and thousands of other languages.
> 
>> Why is it ','.join(iterable), why 
>> isn't there join(',', iterable) At what point does a method become a 
>> global? A member? Do we take the path that everything is a global? Or 
>> should all methods be members? So far it seems arbitrary.
> 
> Okay, its arbitrary.
> 
> Why is it called [].len instead of [].length or {}.size? Why None
> instead of nil or null or nul or NULL or NOTHING?
> 
> Many decisions in programming languages are arbitrary.
> 
In Java, strings have .length(), arrays have .length, and collections 
have .size().


More information about the Python-ideas mailing list