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

谁说我不能喝 提交于 2019-11-27 19:28:41

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

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
gertjan

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.

gerardw

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__) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!