实现保留docstring的类属性

jav*_*vex 4 python docstring properties decorator python-decorators

我有一个描述符,可以将方法转换为类级别的属性:

class classproperty(object):

    def __init__(self, getter):
        self.getter = getter
        self.__doc__ = getter.__doc__

    def __get__(self, instance, owner):
        return self.getter(owner)
Run Code Online (Sandbox Code Playgroud)

像这样使用:

class A(object):
    @classproperty
    def test(cls):
        "docstring"
        return "Test"
Run Code Online (Sandbox Code Playgroud)

不过,我现在不能访问的__doc__属性(这是合乎逻辑的,因为访问A.test.__doc__将会获取__doc__str,因为A.test已经返回"Test".

我的最终目标是我的docstring将出现在sphinx中,因此以任何其他方式检索docstring是不可行的,而不是通过访问attributes __doc__属性.我发现自己想知道这是否有可能以任何方式实现.

我知道property通过在没有实例的情况下调用返回类来解决此问题.但是,显然这与我的目标相冲突.

我开始担心这在Python中是不可能的.

注意:classproperty只要它是稳定的(即不设置__doc__返回的值),我愿意拉出任何特技表演.但是,对用户施加任何负担是不可行的classproperty(即他们应该只使用装饰器并完成它).

Mar*_*ers 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
Run Code Online (Sandbox Code Playgroud)

演示:

>>> 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'
Run Code Online (Sandbox Code Playgroud)

用于:

class A(object):
    @classproperty
    def test(cls):
        return docstring_str("Test", "docstring')
Run Code Online (Sandbox Code Playgroud)

因为str对象是不可变的,所以不能__doc__在装饰器中设置属性.您必须返回一个代理对象,而不是完全包装除__doc__属性之外的实际返回值.这变得复杂而且难看.

另一种方法是property 在元类上定期; 班级:

class MetaClass(type):
    @property
    def test(cls):
        "docstring"
        return "Test"

class A(object):
    __metaclass__ = MetaClass
Run Code Online (Sandbox Code Playgroud)

现在A有一个test属性,docstring可以作为MetaClass.test.__doc__或使用type(A).test.__doc__:

>>> A.test
'Test'
>>> type(A).test
<property object at 0x10757d158>
>>> type(A).test.__doc__
'docstring'
Run Code Online (Sandbox Code Playgroud)

  • Ooo ...元类属性(*做出心理记录*) (2认同)