[Python-Dev] lament for the demise of unbound methods
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 == "<locals>":
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