getcaller#

getcaller(frame=2, tostring=True, includelineno=False, includeline=False, relframe=0, die=False)[source]#

Try to get information on the calling function, but fail gracefully. See also sc.thisfile().

Frame 1 is the file calling this function, so not very useful. Frame 2 is the default assuming it is being called directly. Frame 3 is used if another function is calling this function internally.

Parameters:
  • frame (int) – how many frames to descend (e.g. the caller of the caller of the…), default 2

  • tostring (bool) – whether to return a string instead of a dict with filename and line number

  • includelineno (bool) – if tostring, whether to also include the line number

  • includeline (bool) – if not tostring, also store the line contents

  • relframe (int) – relative frame – another way of specifying the frame; added to “frame”

  • die (bool) – whether to raise an exception if calling information cannot be retrieved

Returns:

the filename (and line number) of the calling function, either as a string or dict

Return type:

output (str/dict)

Examples:

sc.getcaller()
sc.getcaller(tostring=False)['filename'] # Equivalent to sc.getcaller()
sc.getcaller(frame=3) # Descend one level deeper than usual
sc.getcaller(frame=1, tostring=False, includeline=True) # See the line that called sc.getcaller()
New in version 1.0.0.
New in version 1.3.3: do not include line by default
New in version 3.0.0: “relframe” argument; “die” argument