Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Case Statements Date: Thu, 17 Mar 2016 17:48:58 +1100 Lines: 86 Message-ID: References: <30502a2e-0bad-4b0f-a1e8-a2b40b0d7ab9@googlegroups.com> <56E928D4.3000701@rece.vub.ac.be> <56E93ADD.9040500@rece.vub.ac.be> <56E961C9.7040008@rece.vub.ac.be> <56E97B60.3060402@rece.vub.ac.be> <56E9A66E.4030905@rece.vub.ac.be> <56e9f7f9$0$1597$c3e8da3$5496439d@news.astraweb.com> <56ea0e4f$0$1585$c3e8da3$5496439d@news.astraweb.com> <56ea4ed2$0$1522$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de s7x5W/7W60nEpCLK0EgXswK1KnMt7uAj1Vr7HFYMyngw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cpython': 0.05; 'expressions': 0.07; 'implements': 0.07; 'cc:addr:python-list': 0.09; '@property': 0.09; 'situation.': 0.09; 'stack,': 0.09; 'def': 0.13; 'thu,': 0.15; '2016': 0.16; 'equivalence': 0.16; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'documented': 0.18; 'creates': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'first,': 0.20; '"",': 0.22; 'decorator': 0.22; 'function,': 0.22; 'object.': 0.22; 'trying': 0.22; 'second': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'this.': 0.28; '(it': 0.29; 'behaviour': 0.29; 'value)': 0.29; 'anywhere': 0.30; 'work.': 0.30; 'getting': 0.33; 'changed': 0.33; 'class': 0.33; "d'aprano": 0.33; 'decorators': 0.33; 'steven': 0.33; 'traceback': 0.33; 'similar': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'received:209.85.213': 0.37; '(with': 0.38; 'received:209': 0.38; 'anything': 0.38; 'here.': 0.62; 'leaving': 0.63; 'mar': 0.65; 'here': 0.66; 'guaranteed': 0.67; 'brand': 0.75; "'def'": 0.84; 'chrisa': 0.84; 'decorator:': 0.84; 'decorators.': 0.84; 'underneath': 0.84; 'x...': 0.84; 'to:none': 0.91 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; bh=Ll9h8smMF3AwCUCRAkO6iZF4hTmoEg5hhQjCdtuI+k8=; b=EIzC7LQW3Z8gHIcbkb5mcpEsA8eLBGjodxojFI1DsX9+Rwhqj0Egi8tNCEbX1tVJ6b HFXWOr2QvI5RAAodEY1Bq6VkSsg+B3xA4h0ElIdXbH3XuPzBgLquZt7CJT00HEZXDIh2 suCRsS4A/WPACaycwmkXo4nlw/fadFGuWjiOmFS982lfEdlHgk7RNeARBWIhjC41oJGM qQYqg2HY8QHbtN/wahBYBpf+vry6ZRJAO5+wxxIGBntkg0L84sakGi3q4S/iugHXv57Z 79rwY0UTdG0Agdms8ae2IboiKuKvyVVF6zy4p75K452goK7oQPyndAuRDus9s8b+npdK uCWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=Ll9h8smMF3AwCUCRAkO6iZF4hTmoEg5hhQjCdtuI+k8=; b=B0HwNWKLX54c5ULZPnmhVpSGeV1DoM1gckXI6PH6XlrQ4hXddWdlGRGIg0Po2V7H4Z oqn8ywoByCZxRJ3fhcy/FeDG2oxHpQoZK4S92zwqjtbriJfhcqN0g0wWNXaNtYcEdram PzCrLB318bDvYADPc52baRDIABed3dtdFK9KgBOLGtm7i0LLacJHMtIaYqAJb0SZULNl FAyNSa0IXr32VoKYAJPnSs3I/sfuQsRKWKv4vV0QP4CvsiUksflLzlj21Q7YR3+WgXYC gksLfgb5R18qY2aUBmV+Zhwqo1cz76V60MoLxbCnGqKxrcLiRx46AVGdtAsvlXmjvqs6 8IcA== X-Gm-Message-State: AD7BkJI+jlP5sYGOu6zyNj4sGPWGgQpmQLdqFilusuTA2bg6i85rqdu2T8NYGwx7Y4yn+d4oxWgoUA/57R9KdA== X-Received: by 10.50.43.226 with SMTP id z2mr5702283igl.94.1458197338929; Wed, 16 Mar 2016 23:48:58 -0700 (PDT) In-Reply-To: <56ea4ed2$0$1522$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105070 On Thu, Mar 17, 2016 at 5:29 PM, Steven D'Aprano wrote: > I don't think that property is a similar situation. I think what happens > here is that the first call to property sets: > > # @property def x... > x = property(x) > > Then the second decorator does: > > # @x.setter def x... > x = x.setter(x) > > which replaces x with a brand new property object. Okay. Let's try this. >>> class Demo: ... @property ... def x(self): ... print("Getting x") ... return 42 ... @x.setter ... def x(self, value): ... print("Setting x to", value) ... >>> d = Demo() >>> d.x Getting x 42 >>> d.x = 1 Setting x to 1 Decorators work. Now let's try NOT using decorators. >>> class Demo: ... def x(self): ... print("Getting x") ... return 42 ... x = property(x) ... def x(self, value): ... print("Setting x to", value) ... x = x.setter(x) ... Traceback (most recent call last): File "", line 1, in File "", line 8, in Demo AttributeError: 'function' object has no attribute 'setter' Since the 'def' line bound the undecorated function to the name 'x', the decoration underneath *fails*. The only way this could work would be with a temporary name for either the new function or the decorator: >>> class Demo: ... def x(self): ... print("Getting x") ... return 42 ... x = property(x) ... decorator = x.setter ... def x(self, value): ... print("Setting x to", value) ... x = decorator(x) ... del decorator ... >>> d = Demo() >>> d.x Getting x 42 >>> d.x = 1 Setting x to 1 This is how CPython implements this (it evaluates the decoration expressions first, leaving them on the stack, then creates the function, then calls the decorators), but I don't see anywhere that this is guaranteed in the docs either - the only way I know this is current behaviour is from trying it (with dis.dis). The documented equivalence simply doesn't work. Note, by the way, that I am not in any way criticising the *behaviour* here. I don't think anything needs to be changed as regards functionality. And it's not even that big an issue as regards documentation. It's mainly just an esoteric curiosity. ChrisA