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


Groups > comp.lang.python > #78826

Re: operator module functions

From Terry Reedy <tjreedy@udel.edu>
Subject Re: operator module functions
Date 2014-10-08 02:28 -0400
References <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.14671.1412749755.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10/7/2014 9:41 PM, Steven D'Aprano wrote:
> Every Python operator has a function version in the operator module:
>
> operator + has function operator.add;
> operator - has function operator.sub;
> operator * has function operator.mul;
>
> and so forth. Only, that's not quite right... according to the
> documentation, the "official" functions are actually:
>
> operator.__add__;
> operator.__sub__;
> operator.__mul__;
>
> etc., with the underscore-less versions being provided as a convenience.
>
> Was anyone aware of this?

Of course. It has bugged me a bit for years.  Messy.

> Is there anyone who uses or prefers the dunder versions?

I don't and don't, and that seems to be true of other core devs: there 
are no non-test uses of the dunder methods in the stdlib.  Grepping 
/lib/*.py recursively for 'operator.' gives 540 hits, most for 
operator.someop.  Grepping the same for 'operator.__' returns 4 hits for 
operator.__name__ and .__doc__ (not operators) and these 6 tests:

C:\Programs\Python34\Lib\test\test_richcmp.py: 88:     "lt": (lambda 
a,b: a< b, operator.lt, operator.__lt__),
C:\Programs\Python34\Lib\test\test_richcmp.py: 89:     "le": (lambda 
a,b: a<=b, operator.le, operator.__le__),
C:\Programs\Python34\Lib\test\test_richcmp.py: 90:     "eq": (lambda 
a,b: a==b, operator.eq, operator.__eq__),
C:\Programs\Python34\Lib\test\test_richcmp.py: 91:     "ne": (lambda 
a,b: a!=b, operator.ne, operator.__ne__),
C:\Programs\Python34\Lib\test\test_richcmp.py: 92:     "gt": (lambda 
a,b: a> b, operator.gt, operator.__gt__),
C:\Programs\Python34\Lib\test\test_richcmp.py: 93:     "ge": (lambda 
a,b: a>=b, operator.ge, operator.__ge_

If there a back-compatibility excuse?  I cannot remember what operator 
had in 1.4 or whenever it was added, if after that.

-- 
Terry Jan Reedy

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