Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Uri Even-Chen Newsgroups: comp.lang.python Subject: Re: Operator precedence problem Date: Sun, 5 Jun 2016 19:05:16 +0300 Lines: 55 Sender: urievenchen@gmail.com Message-ID: References: <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 5cZdbo4G2uNgrLksN4qDygd7hSg2bd8Wsw74iqh0heyg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.053 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'operator': 0.03; '"""': 0.05; 'python3': 0.05; 'cc:addr:python-list': 0.09; 'def': 0.13; '>>>': 0.15; '2016': 0.16; 'bitwise': 0.16; 'expressions,': 0.16; 'parentheses': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'unary': 0.16; 'wrote:': 0.16; 'sender:addr:gmail.com': 0.18; 'skip:{ 20': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'subject:problem': 0.22; 'cc:no real name:2**0': 0.22; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'arithmetic': 0.29; 'cat': 0.29; 'other,': 0.29; 'url:mailman': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'url:python': 0.33; 'phone:': 0.33; 'url:listinfo': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'url:org': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'thank': 0.38; 'why': 0.39; 'url:en': 0.39; 'url:in': 0.39; 'url:mail': 0.40; 'url:3': 0.60; 'your': 0.60; 'email:': 0.62; '[image:': 0.63; 'url:png': 0.64; 'url:facebook': 0.64; 'website:': 0.65; 'power': 0.72; 'special': 0.73; '3))': 0.84; '512': 0.84; 'ict': 0.84; 'otten': 0.84; 'received:209.85.215.42': 0.84; 'self.value': 0.84; 'url:uri': 0.84; 'url:wisestamp': 0.84; 'utc+5:30,': 0.84; 'url:images': 0.85; 'url:reference': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=jXsc72PkSSu2ZfBDbOvpiCLbsRHbUdwxO0Bi0Q2zaIM=; b=TxOum3E+Wk+tLt5vZe1+/mfi8k5S9ob4ucjQ/Wele4zHwlIkN1lBvmJQRzgvdF/hUB 927L3l+RkmMpMkye9CJOX+5ZWXvIgiVIln73BzwQFvw/iqwN5KtEMm83StDBuMifLaKI iTC7PKR6Hr0RWuIS9uw708KKGIZToDskBkwjnm7RLxXXFg2rostk6x76mOY8l2cmCkKE 3S1uXysJJoMXHwdpj52EF3U5JVkfuLYN/tqmEnf6K+/3tyrrR8d0m8mYMItHQw1zaDrb i5LxmWA7yG3ZdLF7NToWZ/k2u10ddLia3h5Qray4QVVhkvZxz3nAuBt4hEtptmF+E86O PfIg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=speedy-net.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=jXsc72PkSSu2ZfBDbOvpiCLbsRHbUdwxO0Bi0Q2zaIM=; b=vwwhb+mqiH29DQSZ5r51EXDUstX2429hX9TGjiu/ePOrDpphMcw/Z9Qvbaw9FOYbVa s1T/hXY9KGVg0BD5Y6svY/eMTwRVO01xF7TQ+33DtwHnmnKL4Gfl69m7JNY8iJwq6s/t LU3kNeOeru/6PwEThbyOv8DQFcaYJwLe1TP/3LwbLgiUjZfZYU5kAjxnA2PA8sfkrAf+ YVx55XlxWDfddePU6JIHvEkJmwkFuIQNYi5nx0erPuIxzHHNSQTzsvyRou18Wzm7YxOo k8Y+I6zHlEcoMtTZaZvy/TZBhEGzNPYMjETw3FHwiF/YoSL9FRJb5T2f2o/2Ec2mca8U O28g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=jXsc72PkSSu2ZfBDbOvpiCLbsRHbUdwxO0Bi0Q2zaIM=; b=DlFuvsuay5G7z8xNdlmq0NNRhvkAhwTZUrhFq9o+D0/jcT0fCG2vdFlkh4qBF++819 8IvH9UeB1g/H7O1tiRD3qqY49xxD+Y0IM706sc5k0iXEnw05E1RpFwm3DfQtr6lSuunT 3UW80/3BsnltPq6Rq/0KovvKbYp02L94jhG7ibX2l7CGTo9apS0TL2gBJEMe4hT2odr0 V2colIuyIBAUnFSYWI3BgD3xEiw3pexSk5iawBmeHvYeXvyFa1HHv1VGtOaOadYi6QMF vXHqsYhWqljQACrtzuxwijvXWUuv7/b9Uf4gkmxRXnyAJDLTbVyjemf4P9WgqLLAvcze puGA== X-Gm-Message-State: ALyK8tJomwpDzUO1WD3AY5DdP5yV87ajNM1aNRAfgtGHYRB7OGOV2eoUNQj7P1DiYfMsdPMUIPRq4zTLFrz1Qg== X-Received: by 10.25.213.78 with SMTP id m75mr312817lfg.85.1465142756155; Sun, 05 Jun 2016 09:05:56 -0700 (PDT) In-Reply-To: <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> X-Google-Sender-Auth: I5RMWzYVK7qg0mmJCGo8XZUmItc X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <816c651a-d0ae-4e23-a5b2-72a8f7398468@googlegroups.com> <3ff71354-461a-4635-8d1a-c879243e39a8@googlegroups.com> Xref: csiph.com comp.lang.python:109516 My suggestion: Never write expressions, such as 2 ** 3 ** 2 or even 2 * 4 + 5, without parentheses. Always add parentheses - 2 ** (3 ** 2) (or (2 ** 3) **2) or (2 * 4) + 5 (or 2 * (4 + 5)). *Uri Even-Chen* [image: photo] Phone: +972-54-3995700 Email: uri@speedy.net Website: http://www.speedysoftware.com/uri/en/ On Sun, Jun 5, 2016 at 6:05 PM, ICT Ezy wrote: > On Sunday, June 5, 2016 at 1:06:21 PM UTC+5:30, Peter Otten wrote: > > ICT Ezy wrote: > > > > >>>> 2 ** 3 ** 2 > > > Answer is 512 > > > Why not 64? > > > Order is right-left or left-right? > > > > ** is a special case: > > > > """ > > The power operator ** binds less tightly than an arithmetic or bitwise > unary > > operator on its right, that is, 2**-1 is 0.5. > > """ > > https://docs.python.org/3.5/reference/expressions.html#id21 > > > > Here's a little demo: > > > > $ cat arithdemo.py > > class A: > > def __init__(self, value): > > self.value = str(value) > > def __add__(self, other): > > return self._op(other, "+") > > def __pow__(self, other): > > return self._op(other, "**") > > def __repr__(self): > > return self.value > > def _op(self, other, op): > > return A("({} {} {})".format(self.value, op, other.value)) > > $ python3 -i arithdemo.py > > >>> A(1) + A(2) + A(3) > > ((1 + 2) + 3) > > >>> A(1) ** A(2) ** A(3) > > (1 ** (2 ** 3)) > > Thank you very much for your explanation > -- > https://mail.python.org/mailman/listinfo/python-list >