[Python-de] Funktion sucht Aufrufer

Andreas Jung lists at andreas-jung.com
Fre Sep 10 12:13:19 CEST 2004



--On Freitag, 10. September 2004 10:17 Uhr +0200 Bertram Scharpf 
<b.scharpf at tesionmail.de> wrote:

> Hallo,
>
> kann eine Funktion feststellen, von wem sie aufgerufen
> wurde?
> shichang at my-dejanews.com wrote:
>
> Hello.
>    How do I get the caller from within a function?
>

Aus Google:


> Depends on what you want to do:
>
> def current_frame():
>
>     """ Return the current execution frame.
>
>         Be sure to delete this after use because it can introduce
>  circular references.
>     """
>     try:
>  1/0
>     except:
>  frame = sys.exc_info()[2].tb_frame
>     return frame.f_back
>
> You can then step up the execution stack as far as you want
> and grab the locals, globals, whatever from the frame. For details
> see the traceback module in the standard lib.
>
> --
> Marc-Andre Lemburg                               Y2000: 288 days left
> ---------------------------------------------------------------------
>           : Python Pages >>> http://starship.skyport.net/~lemburg/  :
>            ---------------------------------------------------------