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


Groups > comp.lang.python > #96380

Re: Context-aware return

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; 'cpython': 0.05; 'from:addr:yahoo.co.uk': 0.05; 'only,': 0.07; '[1,': 0.09; 'called.': 0.09; 'interpreter,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thrown': 0.09; 'output': 0.13; 'def': 0.13; 'australian': 0.15; 'interpreter': 0.15; '999': 0.16; 'command,': 0.16; 'func():': 0.16; 'ignored,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.16; 'first.': 0.18; 'language': 0.19; 'context.': 0.22; 'lawrence': 0.22; 'pass': 0.22; 'bit': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'equivalent': 0.27; 'function': 0.28; 'prints': 0.29; 'e.g.': 0.30; 'language.': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'me?': 0.34; 'so,': 0.35; 'possible,': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'things': 0.38; 'goes': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'called': 0.40; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'our': 0.64; 'else.': 0.66; 'importantly,': 0.66; 'wish': 0.71; 'procedure:': 0.84; 'pythonistas,': 0.84; 'thing,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Context-aware return
Date Sat, 12 Sep 2015 03:11:50 +0100
References <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 195.147.66.69
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0
In-Reply-To <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.388.1442023924.8327.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1442023924 news.xs4all.nl 23782 [2001:888:2000:d::a6]:58363
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96380

Show key headers only | View raw


On 10/09/2015 18: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?
>

Not unless you were thrown in the Australian equivalent of Broadmoor 
first.  For those who don't know, Broadmoor is a famous place in the UK 
for the criminally insane.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious 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