[Python-Dev] lament for the demise of unbound methods (original) (raw)

Victor Stinner victor.stinner at gmail.com
Thu Jul 4 15:59:23 CEST 2013


2013/7/4 Chris Withers <chris at simplistix.co.uk>:

That doesn't seem helpful as a sensible way to get back to the class object:

globals()[MyClass.method.qualname.split('.')[0]] <class '_main_.MyClass'>

globals() can only be used if MyClass is in the same module. Otherwise, you a more complex function:

import types

def get_function_class(func): obj = func for name in func.qualname.split('.')[:-1]: if name == "": raise ValueError("you lose") if isinstance(obj, types.FunctionType): obj = func.globals[name] else: # get a method of a class, or a class defined in a child obj = getattr(obj, name) return obj

Victor



More information about the Python-Dev mailing list