Search⌘ K
AI Features

Quiz: Introspection and Type-Level Metaprogramming

Test your advanced understanding of Python's runtime introspection, dynamic attribute manipulation, metaclasses, and dynamic code execution safety.

We'll cover the following...
Technical Quiz
1.

Consider the following code snippet involving frame inspection. What is the output of calling func()?

import inspect

def get_caller_var():
    frame = inspect.currentframe().f_back
    return frame.f_locals.get('x', 'Not Found')

def func():
    x = 10
    return get_caller_var()
A.

It raises an AttributeError because currentframe() returns None in most implementations outside of a debugger.

B.

It returns ‘Not Found’ because f_locals returns a copy of the local variables, and x is not yet bound when the frame is inspected.

C.

It returns 10 because f_back accesses the caller’s stack frame, where the local variable x is defined and visible.

D.

It raises a ReferenceError because the frame of func is closed immediately after calling get_caller_var.


1 / 10
...