[Solved] Is there a way to access a variable from a calling function in python?


So after a while of back and forth I have finally found a solution to my issue.

As Matthias suggested I use global to find the object, i decided to use inspect to add it myself like so:

Assigning

def __enter__(self):
    inspect.stack()[1][0].f_globals["_ExampleName"] = self

Retrieving (Fixed)

    @staticmethod
    def _find_example():
        stack = inspect.stack()
        for stack_index in range(2, len(stack)):
            stack_globals = inspect.stack()[stack_index][0].f_globals
            if "_ExampleName" in stack_globals.keys():
                return stack_globals["_ExampleName"]

This is a bit ‘dodgy’ as inspect is not ment to be used in a production environment, However works and solves my issue

0

solved Is there a way to access a variable from a calling function in python?