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


Groups > comp.lang.python > #78832

Re: operator module functions

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 <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'context': 0.07; "'a'": 0.09; 'function,': 0.09; 'integers': 0.09; 'literal': 0.09; 'subject:module': 0.09; 'cc:addr:python-list': 0.11; '"global"': 0.16; 'add.': 0.16; 'dot,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers,': 0.16; 'literal,': 0.16; 'literals.': 0.16; 'syntactic': 0.16; 'variants': 0.16; '(you': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'ones.': 0.31; 'post.': 0.31; 'there.': 0.32; 'text': 0.33; 'definition': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'should': 0.36; 'example,': 0.37; 'two': 0.37; 'easiest': 0.38; 'issue': 0.38; 'pm,': 0.38; 'does': 0.39; 'quote': 0.39; 'space': 0.40; 'how': 0.40; "you're": 0.61; "you've": 0.63; 'show': 0.63; 'name': 0.63; 'provide': 0.64; 'different': 0.65; 'talking': 0.65; 'between': 0.67; "'2'": 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=jAOojwQHmKIUmsH2/FpyzAOHdnqPMBMqGUVcrhqEeMc=; b=o7cB1/N/rWwrxCRCNac7azGePEvGc4oZiXKwqMNRB73pR33wStsHb26fMrnidiuQoR UNmUv/ox6zkk9cGLQKNHIjTktGNfaZkJK/7Ho+A6e8ia4ukfp0g/vwzJsn9LotbIewR+ QhNIjoUdGZYtBRS43rN+M22DPg7bcVo9c6RZ44ZiigmGgEczcEdnfmod1L9v2JYkNIXV 29ilRz6+QX2RxwMMaYRfpmxvGwsoKdtQWZcOjk0E68Yg7iDDbqbQxQNzOv6VqA1n81d4 o6GdxUWv+1Ymi6mRA3hn4RYnl1+VLCsRWh/ZnyPGa9YaijAF0uqSO0/BWjQ1l8MhmUhH f+oA==
MIME-Version 1.0
X-Received by 10.50.66.174 with SMTP id g14mr16415881igt.34.1412760731892; Wed, 08 Oct 2014 02:32:11 -0700 (PDT)
In-Reply-To <facda5ec-ecd9-43d0-99b3-b98f945a9a2d@googlegroups.com>
References <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com> <facda5ec-ecd9-43d0-99b3-b98f945a9a2d@googlegroups.com>
Date Wed, 8 Oct 2014 20:32:11 +1100
Subject Re: operator module functions
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14675.1412760741.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412760741 news.xs4all.nl 2895 [2001:888:2000:d::a6]:54840
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78832

Show key headers only | View raw


(You didn't include any context in your post. Please quote as much
text as would be helpful; it's the easiest way to show what you're
talking about.)

On Wed, Oct 8, 2014 at 7:46 PM,  <marco.nawijn@colosso.nl> wrote:
> For me it makes sense. operator.add should be used in a "global" context
> (I don't know how to express it otherwise). So you provide it with the
> two values that you want to add. The .__add__ variants are bound to a
> particular instance and you provide it with a single value that you want
> to add.

What Steven's talking about is this:

>>> operator.add is operator.__add__
True

It's the exact same function, just accessed with a different name.

> As an example, you cannot use the dunder versions for literals.
>
>>> 2.__add__(3) # Oops, does not work
>>> a = 2
>>> a.__add__(3)
> 5

That's actually just a syntactic issue with integers and the dot. It
works fine if you use any other form of literal, or if you put a space
between the digits and the dot, or use parentheses, or anything; this
is the case with all methods off integers, not just dunder ones.

>>> 2 .__add__(3)
5
>>> (2).__add__(3)
5
>>> 2.0.__add__(3.0)
5.0

The object is exactly the same whether you reference the literal '2'
or the name 'a' that you've bound to it, so its methods must by
definition all be there.

ChrisA

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