context aware execution

Chris Angelico rosuav at gmail.com
Wed Dec 19 17:07:56 CET 2012


On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate <feedbackflow at gmail.com> wrote:
> Hi All !
>
> Is is possible and if yes, is it more easily possible (i am thinking f_back
> maybe) to get the context of the caller when in a function ?
>
> Like to which variable name is this object assigned ?
>
> Or whatever of the callers context that might be of interest.
>
> I want in a function or method determine the context of my caller and adapt
> the functionality accordingly.

First off, please don't! Your code will be *extremely* confusing.

You can find out what function is calling you, eg by throwing an
exception and checking the traceback. My 3AM brain can't think of a
way to get the current thread's traceback without throwing, but there
may well be a way. (I also don't know of a way to get any other
thread's traceback, but that doesn't matter to this discussion.)

But finding out what variable name your return value is to be assigned
to isn't possible at all. For one thing, it might not be assigned
anywhere - it might be being ignored, or it might be used in some
other expression, or anything.

Usually, the best way to adapt to your caller's environment is to be
passed a parameter that specifies the change.

ChrisA



More information about the Python-list mailing list