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

Question:

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

Asked By: Kevin Little

||

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

Answered By: 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
Answered By: 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.

Answered By: 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__) 
Answered By: gerardw

Hello and forgive the necro; but none of these worked for me so inspired by some of the snippets here I cooked up an alternative. This is how I managed to do it for class methods in python 3.7:

import inspect;

def QLNAME(frame):

    code = frame.f_code;
    name = code.co_name;
    qual = None;

    for cls in ( obj for obj in frame.f_globals.values()  #
                     if  inspect.isclass(obj)             ):

        if hasattr(cls, name):

            member = getattr(cls, name);
            if not inspect.isfunction(member): continue;

            if member.__code__ == code:
                qual = member.__qualname__;
                break;

    return qual;

The way it works is actually quite simple, we check

  1. For classes within the frame’s global scope
  2. If this class has a member sharing the name in question
  3. Whether this member is a function
  4. Finally, compare the codes of both functions

If NÂș4 is true, then we know for a fact that cls is a class with some method member that has code identical to the one we get from the frame.

All well and good, but this is not perfect. It won’t work for a property-decorated method far as I’ve tested, and I’m not yet entirely sure how one would handle that corner case. But it is working for me on regular and static methods which is all I need at the moment, so fair enough.

Do note however that this is very slow, though I think that goes without saying. From my own benchmarks, it usually takes around two to almost three seconds per 100,000 calls. This is acceptable for my current use-case, but if you need to use this more than a handful of times in one go, you may want to work out some optimizations first. I’ll leave that in your capable hands (:

Hope someone else who search-engine stumbles here can find some of this useful.

Cheers.

Answered By: Liebranca

The following answer gets the type by utilizing the GC:

Another necro, but this time with a definite answer that has the exactly wanted result.

The idea is to utilize the references that are saved within the GC to the function.
The bottom line: Using the functions defined below, the returned value is the calling class:

import inspect

def get_type_of_calling_func():
    frame = inspect.currentframe().f_back
    func = get_func_from_frame(frame)
    return get_class_from_func(func)

class Class:
    def f(self):
        return get_type_of_calling_func()

print(Class().f())
# <class '__main__.Class'>

The main functionality is given below in two utility functions (the division is for usability). These functions’ definitions are:

import gc
from types import FunctionType, MethodType, FrameType
from typing import Optional, Union, Iterator, Mapping


def get_func_from_frame(frame: FrameType) -> Optional[FunctionType]:
    # Nifty trick - get the function from the reference to its code object
    refs = gc.get_referrers(frame.f_code)

    for ref in refs:
        if isinstance(ref, FunctionType):
            return ref

    return None


def get_class_from_func(func: Union[FunctionType, classmethod, MethodType]) -> Optional[type]:
    cls_qual_name, func_name = func.__qualname__.rsplit('.', 1)
    assert func_name == func.__name__

    # If classmethod or method
    if not isinstance(func, FunctionType):
        self = getattr(func, '__self__', None)
        for possibility in (self, type(self)):
            if isinstance(possibility, type) and possibility.__qualname__ == cls_qual_name:
                return possibility

    # Recursively check GC references
    visited_ids = set()
    items_to_check = [func]

    # The function is referenced within the type's dict, or its descriptor
    while len(items_to_check) > 0:
        obj = items_to_check[0]
        items_to_check = items_to_check[1:]

        obj_id = id(obj)
        if obj_id in visited_ids:
            continue
        visited_ids.add(obj_id)

        refs = gc.get_referrers(obj)
        for ref in refs:
            if isinstance(ref, type) and ref.__qualname__ == cls_qual_name:
                return ref

            if (
                    hasattr(ref, '__get__') or
                    # Then this is a descriptor (e.g. `classmethod`). Check if this works with the descriptor
                    isinstance(ref, Mapping)
                    # Then this is possibly a `__dict__` of a type
            ):
                items_to_check.append(ref)

    return None

Answered By: EZLearner
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.