Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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; 'subject:not': 0.03; 'operator': 0.03; 'binary': 0.07; "subject:' ": 0.07; "'a'": 0.09; 'arguments': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'jan': 0.12; 'assume': 0.14; '(also': 0.16; '*expression*': 0.16; 'boolean': 0.16; 'conditional': 0.16; 'evaluates': 0.16; 'expressions.': 0.16; 'operator.': 0.16; 'overridden': 0.16; 'overriding': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'unlike': 0.19; 'written': 0.21; 'example': 0.22; 'header :User-Agent:1': 0.23; 'equivalent': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'evaluation': 0.30; "skip:' 10": 0.31; 'once,': 0.31; 'yes.': 0.31; 'could': 0.34; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'to:addr:python-list': 0.38; 'flow': 0.39; 'functional': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'called': 0.40; 'expression': 0.60; 'received:173': 0.61; 'here': 0.66; 'evaluate': 0.72; 'special': 0.74; "'and'": 0.84; '(*),': 0.84; 'otten': 0.84; 'overloading': 0.84; 'received:fios.verizon.net': 0.84; 'whereas': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: 'and' is not exactly an 'operator' (was Re: numpy.where() and multiple comparisons) Date: Sat, 18 Jan 2014 19:12:35 -0500 References: <5617a90f-3d9b-48b2-b449-9e5ef4c181e5@googlegroups.com> <52d9e408$0$29769$862e30e2@ngroups.net> <39d6bb6a-ce34-469e-8cb3-24a14331d6c5@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390090373 news.xs4all.nl 2892 [2001:888:2000:d::a6]:57486 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64271 On 1/18/2014 3:50 AM, Peter Otten wrote: > Unlike `&` `and` cannot be overridden (*), > (*) I assume overriding would collide with short-cutting of boolean > expressions. Yes. 'and' could be called a 'control-flow operator', but in Python it is not a functional operator. A functional binary operator expression like 'a + b' abbreviates a function call, without using (). In this case, it could be written 'operator.add(a,b)'. This function, or it internal equivalent, calls either a.__add__(b) or b.__radd__(a) or both. It is the overloading of the special methods that overrides the operator. The control flow expression 'a and b' cannot abbreviate a function call because Python calls always evaluate all arguments first. It is equivalent* to the conditional (control flow) *expression* (also not a function operator) 'a if not a else b'. Evaluation of either expression calls bool(a) and hence a.__bool__ or a.__len__. 'a or b' is equivalent* to 'a if a else b' * 'a (and/or) b' evaluates 'a' once, whereas 'a if (not/)a else b' evaluates 'a' twice. This is not equivalent when there are side-effects. Here is an example where this matters. input('enter a non-0 number :') or 1 -- Terry Jan Reedy