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


Groups > comp.lang.python > #96285

Re: Context-aware return

Path csiph.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'debug': 0.04; 'cc:addr :python-list': 0.09; 'repr': 0.09; 'weird': 0.15; 'command,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ignored,': 0.16; 'return,': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'context.': 0.22; 'sep': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'header :In-Reply-To:1': 0.24; "doesn't": 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'looks': 0.29; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'returning': 0.35; 'something': 0.35; 'but': 0.36; 'depends': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'stuff': 0.38; 'goes': 0.39; 'subject:-': 0.39; 'where': 0.40; 'called': 0.40; 'your': 0.60; 'back': 0.62; 'different': 0.63; 'you.': 0.64; 'else.': 0.66; 'importantly,': 0.66; 'chrisa': 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 :content-type; bh=be8B5tEBdqybUdMlwFd8B9xZhdtBYh1IkDfkVsw8wGg=; b=LnSP7+h3CBLKay8IuIUL80PHHeH6o3DdoveN/0PS2aTRCAk/jQpCjRBYhQ+Q3F1lYO 1Bw+StbPhaehtUKKkyJ2xMTzwLDT+XyiM0IqF/O/To0Vc4cIVdovyA3OM5/IbM5gzMHx 9HOaTqAlLBVkJ4JZg/2iOYNUuG46pOZhHo4LFHiT9TdwinE6UX+g9AQxDAn5+3myC2AM JfRrGgEHOYgn73Os/MnyM5QKifXzgK4O3YvtC8vfZ8pFNWJAxsHJbFmHN8fcfgU3NeFi IxMrJqizNvdafPLi74unG1g5kyHflKqiBG/B7W1SPOyTUQHr7eKsYwXcq7QUTibh3BK1 0Z5Q==
MIME-Version 1.0
X-Received by 10.107.36.8 with SMTP id k8mr61138453iok.157.1441910355841; Thu, 10 Sep 2015 11:39:15 -0700 (PDT)
In-Reply-To <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com>
References <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com>
Date Fri, 11 Sep 2015 04:39:15 +1000
Subject Re: Context-aware return
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.333.1441910364.8327.python-list@python.org> (permalink)
Lines 19
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1441910364 news.xs4all.nl 23774 [2001:888:2000:d::a6]:49962
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96285

Show key headers only | View raw


On Fri, Sep 11, 2015 at 3:54 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> 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.

The first thing that comes to mind is a repr hack. If you make your
function return an int-like object with a different repr, it could
look like it's returning "Awesome" interactively. The usefulness of
that depends on what the two forms are you're trying to return,
though.

Advantage: Doesn't get Ben's cabbage. I think.

Disadvantage: Looks weird if you try to debug stuff and get the repr
staring back at you.

ChrisA

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