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


Groups > comp.lang.python > #96279

Re: Context-aware return

Date 2015-09-10 20:21 +0200
From "Sven R. Kunze" <srkunze@mail.de>
Subject Re: Context-aware return
References <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com> <55F1C5D6.7020202@mail.de>
Newsgroups comp.lang.python
Message-ID <mailman.328.1441909301.8327.python-list@python.org> (permalink)

Show all headers | View raw


I need to add: you need to look up the stack to see if you have been 
called by __main__ and if __main__.__file__ is missing.

Implementation: I would write decorator for your func.

Best,
Sven

PS: did I say it would probably be a bad idea? If not, it would probably 
be a bad idea.

PPS: what is the reason for this special behavior?

On 10.09.2015 20:03, Sven R. Kunze wrote:
> http://stackoverflow.com/questions/2356399/tell-if-python-is-in-interactive-mode 
>
>
>
> On 10.09.2015 19:54, Steven D'Aprano wrote:
>> I have a function which is intended for use at the interactive 
>> interpreter,
>> but may sometimes be used non-interactively. I wish to change it's 
>> output
>> depending on the context of how it is being called.
>>
>> If the function is being called as if it were a procedure or command, 
>> that
>> is the return result is just ignored, I want to return one thing. But 
>> if it
>> is being called where the return result goes somewhere, I want to return
>> something else. Most importantly, I don't want to pass a flag to the
>> function myself, I want the function to know its own context.
>>
>> I don't mind if it is CPython only, or if it is a bit expensive.
>>
>>
>> E.g.
>>
>> def func():
>>      do_stuff()
>>      if procedure:  # FIXME what goes here???
>>          return "Awesome"
>>      else:
>>          return 999
>>
>> Now I can do this:
>>
>>
>> x = func()
>> assert x == 999
>>
>> L = [1, 2, func(), 4]
>> assert L[2] == 999
>>
>> func()
>> # interactive interpreter prints "Awesome"
>>
>> Is such a thing possible, and if so, how would I do it?
>>
>> If I did this thing, would people follow me down the street booing and
>> jeering and throwing things at me?
>
> Probably. ;)
>
> But it it solve a problem, why not.
>
>
> Best,
> Sven

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