Is there a way to find the name of the class a staticmethod belongs to?
class A(object):
@staticmethod
def my_staticmethod():
return 'static'
@classmethod
def my_classmethod():
return 'classmethod'
In [2]: A.my_staticmethod
Out[2]: <function utils.my_staticmethod>
In [3]: A.my_classmethod
Out[3]: <bound method type.my_classmethod of <class 'utils.A'>>
In [4]: A.my_classmethod.im_self.__name__
Out[4]: 'A'
For a classmethod, I can get the name of the class via A.my_classmethod.im_self.__name__
but I could not figure it out for a staticmethod.
Any idea? Thanks.
Well, actually here is what I am trying to do. I am trying to make 2 functions to "serialize/deserialize" a function/classmethod/staticmethod.
That means, someone can pass a function into the serialize function and gets a string.
a_string = serialize(A.my_classmethod)
This string can be stored in the DB for example.. Then, this string should be sufficient to resolve the function to be able to call it:
# later on...
f = deserialize(a_string)
# I can use my function
f(...)
I could make it work for a function or a classmethod but not for a staticmethod since I can't figure out the class it belongs to and use getattr...