Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.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.048 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'operator': 0.03; 'function,': 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; '8bit%:26': 0.16; 'finney': 0.16; 'invocation': 0.16; 'readability': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'skip:f 30': 0.19; 'import': 0.22; 'header :User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'writes:': 0.31; 'allows': 0.31; 'there': 0.35; 'module.': 0.36; 'opposed': 0.36; 'useful': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'is.': 0.60; 'simply': 0.61; 'personal': 0.63; 'such': 0.63; 'refer': 0.63; 'situation': 0.65; 'skip:\xe2 10': 0.65; '8bit%:43': 0.74; 'integrity': 0.74; '\xe2\x80\x93': 0.77; '2014,': 0.84; 'ethan': 0.84; 'furman': 0.84; 'received:125': 0.84; '8bit%:33': 0.91; 'whereas': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: operator module functions Date: Thu, 09 Oct 2014 08:57:15 +1100 References: <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com> <543518a5$0$12989$c3e8da3$5496439d@news.astraweb.com> <543592AE.80807@stoneleaf.us> <1412797764.3704154.176743089.32D63D13@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:ynU3eWUsFmo953H7yoz7ENj63J8= 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412805448 news.xs4all.nl 2905 [2001:888:2000:d::a6]:60484 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78856 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