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.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: References: <54349662$0$12987$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 8 Oct 2014 20:32:11 +1100 Subject: Re: operator module functions From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 (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, 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