Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to retrieve class information from a 'frame' object?

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).

like image 619
Kevin Little Avatar asked Feb 04 '10 21:02

Kevin Little


4 Answers

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..)

like image 108
Clément Avatar answered Oct 18 '22 19:10

Clément


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
like image 32
Ruslan Avatar answered Oct 18 '22 18:10

Ruslan


I just came across this post as I was faced with the same problem. I did not consider the 'self' method an acceptable solution, however, for all the reasons already listed.

The following code demonstrates a different approach: given a frame object it searches the globals for an object with matching member name and code block. The search is hardly exhaustive so it is possible that not all classes will be uncovered, but what classes are found should be the ones we are looking for because we verify matching codes.

Object of the code is to prepend a function name with its class name, if found:

def get_name( frame ):
  code = frame.f_code
  name = code.co_name
  for objname, obj in frame.f_globals.iteritems():
    try:
      assert obj.__dict__[name].func_code is code
    except Exception:
      pass
    else: # obj is the class that defines our method
      name = '%s.%s' % ( objname, name )
      break
  return name

Note the use of __dict__ instead of getattr to prevent catching of derived classes.

Note further that a global search can be avoided if self = frame.f_locals['self']; obj = self.__class__ gives a match, or any obj in self.__class__.__bases__ or deeper, so there is certainly room for optimization / hybridization.

like image 6
gertjan Avatar answered Oct 18 '22 18:10

gertjan


If a method is a class method, the class will be the first argument. This prints out the type of the first arg if present for each calling stack frame:

    def some_method(self):
        for f in inspect.getouterframes(inspect.currentframe() ):
            args, _,_, local_dict = inspect.getargvalues(f[0])
            if args: 
                first_arg = args[0]
                first_value = local_dict[first_arg]
                print(type(first_value).__name__) 
like image 1
gerardw Avatar answered Oct 18 '22 20:10

gerardw