Context-aware return

Chris Angelico rosuav at gmail.com
Thu Sep 10 14:39:15 EDT 2015


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



More information about the Python-list mailing list