我有一个描述符,可以将方法转换为类级别的属性:
class classproperty(object):
def __init__(self, getter):
self.getter = getter
self.__doc__ = getter.__doc__
def __get__(self, instance, owner):
return self.getter(owner)
像这样使用:
class A(object):
@classproperty
def test(cls):
"docstring"
return "Test"
但是,我现在无法访问__doc__
属性(这是合乎逻辑的,因为访问A.test.__doc__
会获取__doc__
的{{1}},因为{{ 1}}已经返回str
。
我的最终目标是我的docstring将出现在sphinx中,因此以任何其他方式检索docstring是不可行的,而不是通过访问属性A.test
属性。我发现自己想知道这是否有可能。
我知道"Test"
通过在没有实例的情况下调用返回类来解决此问题。但是,显而易见的是,这与我的目标相冲突。
我开始担心这在Python中是不可能的。
注意:只要它稳定(即没有在返回的值上设置__doc__
),我愿意在property
中拉出任何噱头。但是,对classproperty
的用户施加任何负担是不可行的(即他们应该只使用装饰器并完成它)。
答案 0 :(得分:5)
实际上,test
是一个返回字符串的属性。你必须子类 str
并赋予__doc__
属性:
class docstring_str(str):
def __new__(cls, v, __doc__=''):
s = super(docstring_str, cls).__new__(cls, v)
s.__doc__ = __doc__
return s
演示:
>>> class docstring_str(str):
... def __new__(cls, v, __doc__=''):
... s = super(docstring_str, cls).__new__(cls, v)
... s.__doc__ = __doc__
... return s
...
>>> s = docstring_str('Test', 'docstring')
>>> s
'Test'
>>> s.__doc__
'docstring'
用作:
class A(object):
@classproperty
def test(cls):
return docstring_str("Test", "docstring')
由于str
个对象是不可变的,因此无法在装饰器中设置__doc__
属性。您必须返回一个代理对象,而不是完全包装除__doc__
属性之外的实际返回值。这变得复杂而且难看。
另一种方法是在元类上放置常规property
;班级的课程:
class MetaClass(type):
@property
def test(cls):
"docstring"
return "Test"
class A(object):
__metaclass__ = MetaClass
现在A
具有test
属性,文档字符串可以MetaClass.test.__doc__
或type(A).test.__doc__
访问:
>>> A.test
'Test'
>>> type(A).test
<property object at 0x10757d158>
>>> type(A).test.__doc__
'docstring'
答案 1 :(得分:2)
如果你跳过一些箍,它可以被检索,但不能直接通过属性本身,如A.test.__doc__
,因为描述符的工作方式。
class classproperty(object):
def __init__(self, getter):
self.getter = getter
def __get__(self, instance, owner):
if instance is None: # instance attribute accessed on class?
return self
return self.getter(owner)
class A(object):
@classproperty
def test(cls):
"test's docstring"
return "Test"
def docstring(cls, methodname):
return getattr(cls, methodname).getter.__doc__
print docstring(A, 'test') # -> test's docstring