Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78856

Re: operator module functions

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: operator module functions
Date 2014-10-09 08:57 +1100
References (1 earlier) <facda5ec-ecd9-43d0-99b3-b98f945a9a2d@googlegroups.com> <543518a5$0$12989$c3e8da3$5496439d@news.astraweb.com> <m1426b$kgq$1@ger.gmane.org> <543592AE.80807@stoneleaf.us> <1412797764.3704154.176743089.32D63D13@webmail.messagingengine.com>
Newsgroups comp.lang.python
Message-ID <mailman.14693.1412805448.18130.python-list@python.org> (permalink)

Show all headers | View raw


random832@fastmail.us writes:

> On Wed, Oct 8, 2014, at 15:38, Ethan Furman wrote:
> > The main reason I bother using the operator module is for the
> > readability of not seeing the dunders, and the writability of not
> > having to type them.
>
> I'm not sure what situation you would have to type them (as opposed to
> simply a + b) that the operator module would help with.

Any situation where you need to refer to a function. ‘+’ is not a
function, whereas ‘operator.add’ is.

    import operator
    import functools

    add_three = functools.partial(operator.add, 3)

    foo = list(range(10))
    bar = map(add_three, foo)

The above ‘map’ invocation – which is useful because it allows any
arbitrary one-parameter function to be used – cannot be used with ‘+’,
which is not a function.

Likewise, ‘functools.partial’ requires a function, and ‘+’ is not a
function.

There are countless such uses for the functions in the ‘operator’
module.

-- 
 \      “It is the integrity of each individual human that is in final |
  `\        examination. On personal integrity hangs humanity's fate.” |
_o__)               —Richard Buckminster Fuller, _Critical Path_, 1981 |
Ben Finney

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

operator module functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-08 12:41 +1100
  Re: operator module functions Terry Reedy <tjreedy@udel.edu> - 2014-10-08 02:28 -0400
  Re: operator module functions marco.nawijn@colosso.nl - 2014-10-08 01:46 -0700
    Re: operator module functions Chris Angelico <rosuav@gmail.com> - 2014-10-08 20:32 +1100
      Re: operator module functions Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-09 10:37 +1300
        Re: operator module functions Chris Angelico <rosuav@gmail.com> - 2014-10-09 09:30 +1100
        Re: operator module functions Chris Kaynor <ckaynor@zindagigames.com> - 2014-10-08 15:40 -0700
    Re: operator module functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-08 21:57 +1100
      Re: operator module functions Terry Reedy <tjreedy@udel.edu> - 2014-10-08 15:09 -0400
      Re: operator module functions Ethan Furman <ethan@stoneleaf.us> - 2014-10-08 12:38 -0700
      Re: operator module functions random832@fastmail.us - 2014-10-08 15:49 -0400
      Re: operator module functions Gelonida N <gelonida@gmail.com> - 2014-10-08 23:05 +0200
      Re: operator module functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-08 15:23 -0600
      Re: operator module functions Ethan Furman <ethan@stoneleaf.us> - 2014-10-08 14:49 -0700
      Re: operator module functions Chris Kaynor <ckaynor@zindagigames.com> - 2014-10-08 14:21 -0700
      Re: operator module functions Ben Finney <ben+python@benfinney.id.au> - 2014-10-09 08:57 +1100
      Re: operator module functions Terry Reedy <tjreedy@udel.edu> - 2014-10-08 18:46 -0400
      Re: operator module functions Ethan Furman <ethan@stoneleaf.us> - 2014-10-08 16:35 -0700
      Re: operator module functions Ned Batchelder <ned@nedbatchelder.com> - 2014-10-08 21:41 -0400

csiph-web