0

I need to get the class (not just the name, the real class ref) from an inspect stack return.

Say i have this:

def methodthatneedcls():
    cls = ### code here


class excls:
    def somemethod():
        methodthatneedcls()
         
        something = "is happening here"
        return something

if i go back with inspect.stack() i want to get the class reference of the previous entry in the stack, in this case the excls reference. I looked into to the return of inspect.stack() but i didnt find anything useful there. If there is an alternative method that allows me to get the class reference of the previous stack it is also welcomened

NoBlockhit
  • 369
  • 2
  • 15
  • I found a way to find the class of a bound method, so far so good, but i cant find a function reference from the FrameInfo or the frame object itself – NoBlockhit Aug 27 '22 at 20:15
  • 1
    Inside `somemethod` (assuming you turn it into a regular method), the expression `type(inspect.stack()[0].frame.f_locals['self'])` evaluates to the class. What do you mean "a function reference..."? – nickie Aug 27 '22 at 20:23
  • When is it that you want to call `inspect.stack()` (or something more suitable)? You don't show if the method `somemethod()` (did you mean: `def somemethod(self):`?) is being called. – quamrana Aug 27 '22 at 20:35
  • Ok i managed to achieve what i wanted, imma send it in here later, cant do it rn – NoBlockhit Aug 28 '22 at 09:26

0 Answers0