[파이썬] `sys._getframe()`: 현재 또는 지정된 프레임 반환
import sys

# Getting the current frame
current_frame = sys._getframe()
print(f"Current Frame: {current_frame}")

# Accessing frame information
current_line = current_frame.f_lineno
current_file = current_frame.f_code.co_filename
print(f"Current Line: {current_line}")
print(f"Current File: {current_file}")

# Accessing local variables in the current scope
local_variables = current_frame.f_locals
print(f"Local Variables: {local_variables}")

In the above example, we first import the sys module. Then we use sys._getframe() to retrieve the current frame and assign it to the current_frame variable. We print the frame information such as the frame object itself, the current line of code, and the file name.

We can also access the local variables within the current scope by accessing the f_locals attribute of the frame object. In this example, we print the local variables in the current_frame using current_frame.f_locals.

Note that using sys._getframe() is generally not recommended in production code unless you have a specific use case where the frame information is necessary. Its usage is more common in debugging or tracing scenarios.

Please keep in mind that accessing the frame object and manipulating the execution stack can have performance implications and should be used judiciously.