Sven R. Kunze
srkunze at mail.de
Thu Sep 10 20:21:36 CEST 2015
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.
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:
> On 10.09.2015 19:54, Steven D'Aprano wrote:
>> I have a function which is intended for use at the interactive
>> but may sometimes be used non-interactively. I wish to change it's
>> depending on the context of how it is being called.
>> If the function is being called as if it were a procedure or command,
>> 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.
>> def func():
>> if procedure: # FIXME what goes here???
>> return "Awesome"
>> return 999
>> Now I can do this:
>> x = func()
>> assert x == 999
>> L = [1, 2, func(), 4]
>> assert L == 999
>> # 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.
More information about the Python-list