News for hackers. Stuff that matters.
Re:The unknown features of Python's `operator` module by stuaxo on Sunday August 08, 2021 @09:53AM (#28106704)
I've used one or two of these before, would be interested in more patterns that use, methodcaller (I think in the example given str.strip would have worked too).
Re:The unknown features of Python's `operator` module by mjw1007 on Sunday August 08, 2021 @10:36AM (#28107007)
In my experience methodcaller is an occasionally-useful "mid-refactoring feature".

That is, sometimes when I'm making a largeish modification there's a point in the commit series where the natural way to express what's going on involves using methodcaller, then by the time the work is finished the use has gone away again.

Re:The unknown features of Python's `operator` module by klyrs on Sunday August 08, 2021 @11:35AM (#28107459)
methodcaller can be useful in functional programming, like

  close_file = methodcaller('close')
  exhaust(map(close_file, files))
where exhaust is a function that consumes an iterator. Obviously this is a contrived example, but it's a weird transformation -- it turns map from 'call this function on each of a sequence of arguments' to, roughly 'call each of this sequence of functions on a fixed argument'
Re:The unknown features of Python's `operator` module by wruza on Sunday August 08, 2021 @02:28PM (#28108900)
It’s higher order for the sake of higher order, and unreadable as hell when you return to five full pages of this nonsense a year later. These getter-caller generators are cool to show off, but irl the most naturally readable form is either a plain anonymous function, or a special form suggested by a sibling commenter.

Both by a compiler easily readable by a human unreadable out of purely functional languages by design this high fashion came.

Re:The unknown features of Python's `operator` module by klyrs on Sunday August 08, 2021 @07:55PM (#28111311)
Sometimes you need to do higher order stuff (or, maybe you don't? But it exists for good reason)

Do you think it's better to use a conceptually difficult example to showcase a high order function? Or do you think it's better to show an example where the reader can accurately understand the concept and focus on what the function does?

Re:The unknown features of Python's `operator` module by xrisk on Sunday August 08, 2021 @12:26PM (#28107872)
exhaust((file.close() for file in files)) works.
Re:The unknown features of Python's `operator` module by klyrs on Sunday August 08, 2021 @12:40PM (#28108015)
Like I said, contrived example. Though, it's a specific example of a more general pattern where you have a bunch of objects that you need to send a message to. You might use this for event handling in the guts of a game engine.
Re:The unknown features of Python's `operator` module by elteto on Sunday August 08, 2021 @04:30PM (#28109826)
I believe exhaust(file.close() for file in files) should work as well, no need for the extra parens.