views:

169

answers:

2

Is it possible to retrieve any class information from a frame object? I know how to get the file (frame.f_code.co_filename), function (frame.f_code.co_name) and line number (frame.f_lineno), but would like to be able to also get the name of the class of the active object instance of the frame (or None if not in an instance).

+2  A: 

I don't believe that, at the frame object level, there's any way to find the actual python function object that has been called.

However, if your code rely on the common convention : naming the instance parameter of a method self, then you could do the following :

def get_class_from_frame(fr):
  import inspect
  args, _, _, value_dict = inspect.getargvalues(fr)
  # we check the first parameter for the frame function is
  # named 'self'
  if len(args) and args[0] == 'self':
    # in that case, 'self' will be referenced in value_dict
    instance = value_dict.get('self', None)
    if instance:
      # return its class
      return getattr(instance, '__class__', None)
  # return None otherwise
  return None

If you don't want to use getargvalues, you can use directly frame.f_locals instead of value_dict and frame.f_code.co_varnames[:frame.f_code.co_argcount] instead of args.

Keep in mind that this is still only relying on convention, so it is not portable, and error-prone:

  • if a non-method function use self as first parameter name, then get_class_from_frame will wrongly return the class of the first parameter.
  • it can be misleading when working with descriptors (it will return the class of the descriptor, not of the actual instance being accessed).
  • @classmethod and @staticmethod won't take a self parameter and are implemented with descriptors.
  • and surely a lot more

Depending on what exactly you want to do, you might want to take some time to dig deeper and find workarounds for all these issues (you could check the frame function exist in the returned class and share the same source, detecting descriptor calls is possible, same for class methods, etc..)

Clément
Thanks for your answer, Clément; it is detailed and thoughtful. I'm still hoping to get more than just one response! But if not, this one will have to suffice...
Kevin Little
OK, waited long enough. This is a good answer! I had hoped for better support from the frame structures, but it is what it is. Thanks again, Clément!
Kevin Little
+1  A: 

This is a bit shorter, but does about the same. Returns None if class name not available.

def get_class_name():
    f = sys._getframe(1)

    try:
        class_name = f.f_locals['self'].__class__.__name__
    except KeyError:
        class_name = None

    return class_name
Ruslan