Python sys.gettrace() Method

Python sys.gettrace() Method

  1. Syntax of Python sys.gettrace() Method
  2. Example Codes: Working With the sys.gettrace() Method

The sys.gettrace() method is an efficient way of getting the traceback call to the platform’s Python interpreter. A traceback call is the information returned when an event occurs in the code.

Syntax of Python sys.gettrace() Method

sys.gettrace()

Parameters

No parameter is required. It is a non-callable object.

Return

The return type of this method is the trace function as set by the sys.settrace() method.

Example Codes: Working With the sys.gettrace() Method

import sys

from sys import settrace

def trace(frame , event, arg):

    code = frame.f_locals["a"]

    if code % 2 == 0:

        frame.f_locals["a"] = code

def f(a):

    print (a)

if __name__ == "__main__":

    sys.settrace(trace)

    for x in range(0,10):

        f(x)

print(sys.gettrace())

Output:

0
1
2
3
4
5
6
7
8
9
<function trace at 0x7f344f73a280>

In the above code, we modified the arguments sent to a function. The function uses a for loop and counts the digit in a specified range.

After setting the system trace call, we used the sys.gettrace() method to return the traceback call.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Sys

  • Python sys.stdin Attribute
  • Python sys.argv List
  • Python sys.maxsize() Method
  • Python sys.stdout Method
  • Python sys.exit() Method
  • Python sys.path Attribute