Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; 'cpython': 0.05; 'only,': 0.07; '[1,': 0.09; 'called.': 0.09; 'interpreter,': 0.09; 'output': 0.13; 'stack': 0.13; 'def': 0.13; 'interpreter': 0.15; '999': 0.16; 'command,': 0.16; 'func():': 0.16; 'ignored,': 0.16; 'wrote:': 0.16; 'not,': 0.22; 'context.': 0.22; 'decorator': 0.22; 'pass': 0.22; 'bit': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'not.': 0.27; 'function': 0.28; 'idea?': 0.29; 'prints': 0.29; 'e.g.': 0.30; 'probably': 0.31; 'skip:_ 10': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'me?': 0.34; 'received:10.0': 0.34; 'so,': 0.35; 'best,': 0.35; 'possible,': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'being': 0.37; 'say': 0.37; 'things': 0.38; 'why': 0.39; 'goes': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'called': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'else.': 0.66; 'importantly,': 0.66; 'wish': 0.71; 'special': 0.73; 'add:': 0.84; 'behavior?': 0.84; 'procedure:': 0.84; 'thing,': 0.93 Date: Thu, 10 Sep 2015 20:21:36 +0200 From: "Sven R. Kunze" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Context-aware return References: <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com> <55F1C5D6.7020202@mail.de> In-Reply-To: <55F1C5D6.7020202@mail.de> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 1849 X-purgate-ID: 154282::1441909299-00000776-BFCA0EFC/0/0 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441909301 news.xs4all.nl 23754 [2001:888:2000:d::a6]:33874 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5141 X-Received-Body-CRC: 715750161 Xref: csiph.com comp.lang.python:96279 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