How to use self-inspection to check for try-block

Chris Rebert clp2 at rebertia.com
Fri Mar 20 04:44:59 EDT 2009


On Fri, Mar 20, 2009 at 1:32 AM,  <elmar at cmbi.ru.nl> wrote:
> Hi everyone,
>
> is there a sufficiently easy possibility for a Python function to find
> out whether it has been called from a try-block or not?
>
> try:
>  print "Calling foo"
>  foo()
> except:
>  print "Got exception"
>
> In the example above, foo() should be able to 'see' that it was called
> from a try block, allowing it to behave differently.
>
> Can this information be obtained from the traceback/frame/code
> objects, or is that too difficult?

It might be possible, but it seems like there ought to be a better way
to accomplish your goal. Could you explain why you want to do this in
the first place? Perhaps a better alternative can be found.

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list