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


Groups > comp.lang.python > #105086

Re: Case Statements

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Case Statements
Date 2016-03-17 22:49 +1100
Message-ID <mailman.268.1458215384.12893.python-list@python.org> (permalink)
References (17 earlier) <56ea0e4f$0$1585$c3e8da3$5496439d@news.astraweb.com> <dkuujrFcplnU1@mid.individual.net> <56ea4ed2$0$1522$c3e8da3$5496439d@news.astraweb.com> <mailman.257.1458197342.12893.python-list@python.org> <56ea8c97$0$1607$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On Thu, Mar 17, 2016 at 9:53 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Thu, 17 Mar 2016 05:48 pm, Chris Angelico wrote:
>
>> Okay. Let's try this.
> [...]
>> Decorators work. Now let's try NOT using decorators.
>
> You are still using a decorator. You're just not using @ decorator syntax.

Oops, sorry. That was sloppy wording on my part. What I meant was
"decorator syntax".

The rest of your analysis was equally correct.

> But we can fix it like this:
>
>     def x(self):
>         print("Getting x")
>         return 42
>     x = property(x)
>     def y(self, value):
>         print("Setting x to", value)
>     x = x.setter(y)

Oh... I thought x.setter() needed the name to be the same. Okay, in
that case it's not so hard. Doesn't need the messy temporaries of the
other option.

>> 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.
>
> It does work. You just have to be careful to not garbage collect objects
> before you can use them :-)

The "documented equivalence" is what's written here:

https://docs.python.org/3/reference/compound_stmts.html#function
"""
For example, the following code

@f1(arg)
@f2
def func(): pass

is equivalent to

def func(): pass
func = f1(arg)(f2(func))
"""

Saying "is equivalent to" implies a correlation that, in this case, is
imperfect. But to be fair, these kinds of edge cases are (a) only of
interest to language developers and the curious, and (b) exist in
quite a few places in the language. The correspondence between
operators and magic methods [1] has the same flaw; saying that x<y
calls x.__lt__(y) is not entirely accurate, but is much more useful to
people than x.__class__.__lt__(x, y) (or is it "type(x).__lt__(x, y)"?
I can never remember what the differences are between type() and
__class__), so it's correct to use that in the docs. Maybe there needs
to be a collection of "language developer esoteria", primarily for the
benefit of alternate implementations? It could enumerate the ways in
which CPython differs from the plain and simple docs, and say which
ones are important language features and which are implementation
details.

ChrisA

[1] https://docs.python.org/3/reference/datamodel.html#object.__lt__

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Case Statements jj0gen0info@gmail.com - 2016-03-15 13:46 -0700
  Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 23:11 +0000
  Re: Case Statements jj0gen0info@gmail.com - 2016-03-15 16:47 -0700
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 23:58 +0000
    Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 00:51 +0000
      Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 01:05 +0000
  Re: Case Statements jj0gen0info@gmail.com - 2016-03-15 18:55 -0700
    Re: Case Statements "Mario R. Osorio" <nimbiotics@gmail.com> - 2016-03-15 21:06 -0700
      Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 10:34 +0000
        Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-16 23:56 +1100
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 04:26 +0000
      Re: Case Statements Christian Gollwitzer <auriocus@gmx.de> - 2016-03-16 09:13 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 08:47 +0000
        Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 11:16 +0200
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 10:35 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 09:51 +0000
          Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 19:41 +0000
            Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 22:30 +0000
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 11:52 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 11:07 +0000
          Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 11:16 +0000
            Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 11:33 +0000
            Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 14:21 +0200
              Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 12:53 +0000
                Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 16:31 +0200
                Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 15:00 +0000
                Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 19:07 +0200
              Re: Case Statements Rustom Mody <rustompmody@gmail.com> - 2016-03-20 01:01 -0700
                Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-20 08:29 +0000
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 14:38 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 14:02 +0000
        Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 21:27 +0200
          Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:22 +0100
            Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 10:57 +0200
              Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 10:12 +0100
        Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 11:19 +1100
          Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 12:54 +1100
            Re: Case Statements Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-17 18:45 +1300
              Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 17:30 +1100
                Re: Case Statements Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-18 18:59 +1300
              Re: Case Statements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-17 17:29 +1100
                Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 17:48 +1100
                Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 21:53 +1100
                Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 22:49 +1100
            Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 10:23 +0100
            Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 20:55 +1100
          Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:36 +0100
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:38 +0100
  Re: Case Statements l0r0m0a0i0l@gmail.com - 2016-03-16 06:15 -0700
    Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 00:27 +1100
      Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 14:00 +0000
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 13:59 +0000
  Re: Case Statements l0r0m0a0i0l@gmail.com - 2016-03-16 07:21 -0700
    Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 16:33 +0200

csiph-web