Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'syntax': 0.04; 'variables': 0.07; 'correspond': 0.09; 'function,': 0.09; 'implements': 0.09; 'obsolete': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'trailing': 0.09; 'underscore': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; 'mostly': 0.14; '"official"': 0.16; 'methods;': 0.16; 'operators.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'self.y': 0.16; 'symbols': 0.16; 'variants': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; "skip:' 10": 0.31; "d'aprano": 0.31; 'names.': 0.31; 'operators': 0.31; 'steven': 0.31; 'class': 0.32; 'received:fr': 0.33; 'table': 0.34; '"the': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'there': 0.35; 'curious': 0.36; 'functions.': 0.36; 'module.': 0.36; 'shows': 0.36; 'should': 0.36; 'implement': 0.38; 'mapping': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'how': 0.40; 'remove': 0.60; 'eventually': 0.60; 'received:109': 0.72; 'special': 0.74; 'duplication': 0.84; 'etc,': 0.84; 'overloading': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: operator module functions Date: Wed, 08 Oct 2014 23:05:08 +0200 References: <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com> <543518a5$0$12989$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 1-75-190-109.dsl.ovh.fr User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412802326 news.xs4all.nl 2915 [2001:888:2000:d::a6]:33067 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78850 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. So for implemetation you use __add__ for using you use the operator + if you need a class MyClass(object): def __init__(self, x, y): self.x = x self.y = y # implements + for a certain type def __add__(self, other): return MyClass(self.x + other.x, self.y + other.y) def __repr__(self): return "(%f,%f)" % (self.x, self.y) a = MyClass(1,2) b = MyClass(4,5) print(a) print(b) print(a+b) # operator.add is identical to the + operator , # BUT it is a function and can thuss be # assigned to variables passed as parameter def dosomething(op, val1, val2): return op(val1, val2) print(dosomething(operator.add, 1, 2)) print(dosomething(operator.add, a, b))