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: 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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Sep 11, 2015 at 3:54 AM, Steven D'Aprano 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