Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78855
| References | <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com> <facda5ec-ecd9-43d0-99b3-b98f945a9a2d@googlegroups.com> <543518a5$0$12989$c3e8da3$5496439d@news.astraweb.com> <m1426b$kgq$1@ger.gmane.org> <m148u6$iu9$1@ger.gmane.org> |
|---|---|
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | 2014-10-08 14:21 -0700 |
| Subject | Re: operator module functions |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14692.1412804996.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Wed, Oct 8, 2014 at 2:05 PM, Gelonida N <gelonida@gmail.com> wrote: > On 10/8/2014 9:09 PM, Terry Reedy wrote: > >> On 10/8/2014 6:57 AM, Steven D'Aprano wrote: >> >> According to the documentation, operator.__add__ is the "official" >>> function, >>> and operator.add is just there for convenience. >>> >> >> You are paraphrasing "The function names are those used for special >> class methods; variants without leading and trailing __ are also >> provided for convenience." But then there is the following: >> >> 10.3.1. Mapping Operators to Functions >> >> This table shows how abstract operations correspond to operator symbols >> in the Python syntax and the functions in the operator module. >> Operation Syntax Function >> Addition a + b add(a, b) >> >> etc, using the 'convenient' names. I would like to deprecate and >> eventually remove the dunder names. To me, the duplication is not >> 'convenient'. >> >> > > I'd be curious about a proposal to obsolete the double underscore > functions and just keep operator.add or to just keep the operator + > > For me they seem rather distinct, though they are interchangable in > certain situations. > > Perhaps I got something wrong, but all dunder functions are 'magic' > functions. Many of them mostly should ne used when when overloading > behaviour or when creating a class which should implement operators. > > print(dosomething(operator.add, 1, 2)) > print(dosomething(operator.add, a, b)) The thing being purposed to remove, is that you can also do: print(dosomething(operator.__add__, 1, 2)) print(dosomething(operator.__add__, a, b)) Not that fact that you can define __add__ in the class, or that "operator.add(a, b)" is the same as "a + b" (all of which are quite useful). Basically, the operator module has a "__add__" function in addition to a "add" function, and they are both identical (in fact, they are the same function object): >>> import operator >>> operator.add <built-in function add> >>> operator.__add__ <built-in function add> >>> operator.__add__ is operator.add True
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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