Given a function which is a staticmethod
of a class, is there a way to extract the parent class object from this?
class A:
@staticmethod
def b(): ...
...
f = A.b
...
assert get_parent_object_from(f) is A
I can see this buried in the __qualname__
, but can't figure out how to extract this.
The function get_parent_object_from
should have no knowledge of the parent class A
.
One approach would be to use a custom static method descriptor that sets the owner class as an attribute of the wrapped function:
class StaticMethod(staticmethod):
def __set_name__(self, owner, name):
self.__wrapped__.owner = owner
class A:
@StaticMethod
def b(): ...
f = A.b
assert f.owner is A
Demo here