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


Groups > comp.lang.python > #96468

Re: Context-aware return

Newsgroups comp.lang.python
Date 2015-09-12 16:27 -0700
References <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com> <mst80l$1lv$1@dont-email.me>
Message-ID <c52d15cd-c043-47d3-859c-fdeea87f08a5@googlegroups.com> (permalink)
Subject Re: Context-aware return
From Ned Batchelder <ned@nedbatchelder.com>

Show all headers | View raw


On Thursday, September 10, 2015 at 8:44:01 PM UTC-4, Denis McMahon wrote:
> On Fri, 11 Sep 2015 03:54:14 +1000, Steven D'Aprano wrote:
> 
> > If I did this thing, would people follow me down the street booing and
> > jeering and throwing things at me?
> 
> Yes
> 
> >>> x = func()
> >>> x
> >>> func()
> >>> print x == func() 
> >>> assert x == func()
> 
> Would you expect the last two calls to func() to return 999 or "Awesome"? 
> Why? What is the material difference if any between interpreter (a) 
> displaying the return value and (b) comparing the return value with 
> another value.
> 
> Debugging nightmare!

I'll add my voice to the rising chorus condemning the very notion
of a function that behaves this way!

Then, I'll give you an implementation (Python 2):

    import inspect
    import opcode


    def magic_return():
        frame = inspect.stack()[1][0]
        code = frame.f_code
        next_opcode = opcode.opname[ord(code.co_code[frame.f_lasti+3])]
        if next_opcode == "PRINT_EXPR":
            ret = "Used at the prompt"
        elif next_opcode == "POP_TOP":
            ret = "Value ignored"
        else:
            ret = "Normal call"
        print ret
        return ret

    def try_it():
        magic_return()
        x = magic_return()
        print magic_return()
        magic_return() + ""

This examines the byte code of the caller to determine the next
byte code after the CALL_FUNCTION that called us.  The byte code
used next shows what will happen to the return value.

Try it out:

    $ python -i detect_caller.py
    >>> magic_return()
    Used at the prompt
    'Used at the prompt'
    >>> try_it()
    Value ignored
    Normal call
    Normal call
    Normal call
    Normal call
    >>>

I'm sure there are plenty of cases this gets wrong.  If you try
to pin this on me, I will swear up and down that someone hacked
into my account to send this message...

--Ned.

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


Thread

Context-aware return Steven D'Aprano <steve@pearwood.info> - 2015-09-11 03:54 +1000
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:03 +0200
  Re: Context-aware return Ben Finney <ben+python@benfinney.id.au> - 2015-09-11 04:12 +1000
  Re: Context-aware return Ben Finney <ben+python@benfinney.id.au> - 2015-09-11 04:14 +1000
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:21 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:24 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:34 +0200
  Re: Context-aware return Chris Angelico <rosuav@gmail.com> - 2015-09-11 04:39 +1000
  Re: Context-aware return Laura Creighton <lac@openend.se> - 2015-09-10 20:40 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:45 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:56 +0200
  Re: Context-aware return Paul Rubin <no.email@nospam.invalid> - 2015-09-10 12:19 -0700
  Re: Context-aware return Grant Edwards <invalid@invalid.invalid> - 2015-09-10 19:23 +0000
    Re: Context-aware return Akira Li <4kir4.1i@gmail.com> - 2015-09-10 23:15 +0300
      Re: Context-aware return Grant Edwards <invalid@invalid.invalid> - 2015-09-10 20:27 +0000
    Re: Context-aware return random832@fastmail.us - 2015-09-10 16:42 -0400
    Re: Context-aware return Rustom Mody <rustompmody@gmail.com> - 2015-09-11 08:55 -0700
    Re: Context-aware return Rustom Mody <rustompmody@gmail.com> - 2015-09-11 08:55 -0700
  Re: Context-aware return Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-11 00:41 +0000
    Re: Context-aware return Ned Batchelder <ned@nedbatchelder.com> - 2015-09-12 16:27 -0700
      Re: Context-aware return Steven D'Aprano <steve@pearwood.info> - 2015-09-17 12:31 +1000
  Re: Context-aware return Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-12 03:11 +0100

csiph-web