staticmethod가 속한 클래스의 이름을 찾는 방법이 있습니까?staticmethod : 클래스 이름을 가져 옵니까?
classmethod를 들어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'
, 나는 A.my_classmethod.im_self.__name__
를 통해 클래스의 이름을 얻을 수 있지만 나는 StaticMethod를 위해 그것을 알아낼 수 없었다.
아이디어가 있으십니까? 감사합니다. .
글쎄, 실제로 여기 내가하려는 일이 있습니다. function/classmethod/staticmethod를 "serialize/deserialize"하는 두 개의 함수를 만들려고합니다.
즉 누군가가 serialize 함수에 함수를 전달하고 문자열을 가져올 수 있습니다.
# later on...
f = deserialize(a_string)
# I can use my function
f(...)
나는 그것이 작동되도록 수 :
a_string = serialize(A.my_classmethod)
이 문자열은 다음 예를 들어 DB .. 에 저장할 수 있습니다,이 문자열은 호출 할 수있는 기능을 해결하기 위해 충분해야한다 함수 나 클래스 메쏘드에 대해서는 정적 메쏘드가 아니고 getattr을 사용하는 클래스를 알아낼 수 없으므로 static 메쏘드가 아닙니다. ...
가능한 복제본 [파이썬에서 정적 메서드 내에서 클래스 이름을 얻는 방법] (http://stackoverflow.com/questions/4691925/in-python-how-to-get-name-of-a- static-method-class-inside-its-static-method) – BrenBarn
기본적으로 staticmethod의 전체적인 점은 클래스에 대한 연결이 없다는 것입니다. 클래스가 어떤 클래스인지 알고 싶으면 정적 메서드를 사용하지 마십시오. – BrenBarn
도 여기에 복사 : http://stackoverflow.com/questions/949259/how-do-i-infer-the-class-to-which-a-staticmethod-belongs – gnr