Python继承docstring错误'只读'

时间:2016-07-26 21:28:59

标签: python python-2.7 python-decorators

我已经阅读了关于这个主题的所有其他帖子,但由于他们中的大多数已经很老了,我觉得最好用我自己的问题打开一个新的,因为那里提出的解决方案对我不起作用但我还没有在评论中看到任何投诉。

例如,提议的解决方案here给了我错误:

AttributeError: 'NoneType' object attribute '__doc__' is read-only

第二个提出的解决方案there给了我:

TypeError: Error when calling the metaclass bases
    readonly attribute

我发现有关docstring为只读的唯一参考是here

我正在使用Python 2.7.9。我做错了什么或 doc 属性真的只读?

这是SSCCE的另一个解决方案之一:

# decorator proposed in https://stackoverflow.com/a/8101598/551045
def fix_docs(cls):
    for name, func in vars(cls).items():
        if not func.__doc__:
            print func, 'needs doc'
            for parent in cls.__bases__:
                parfunc = getattr(parent, name)
                if parfunc and getattr(parfunc, '__doc__', None):
                    func.__doc__ = parfunc.__doc__
                    break
    return cls



class X(object):
    """
    some doc
    """

    def please_implement(self):
        """
        I have a very thorough documentation
        :return:
        """
        raise NotImplementedError


@fix_docs
class SpecialX(X):

    def please_implement(self):
        return True


print help(SpecialX.please_implement)

输出:

Traceback (most recent call last):
None needs doc
  File "C:/Users/RedX/.PyCharm2016.2/config/scratches/scratch.py", line 29, in <module>
    class SpecialX(X):
  File "C:/Users/RedX/.PyCharm2016.2/config/scratches/scratch.py", line 9, in fix_docs
    func.__doc__ = parfunc.__doc__
AttributeError: 'NoneType' object attribute '__doc__' is read-only

使用固定装饰器:

import types

def fix_docs(cls):
    for name, func in vars(cls).items():
        if isinstance(func, types.FunctionType) and not func.__doc__:
            print func, 'needs doc'
            for parent in cls.__bases__:
                parfunc = getattr(parent, name, None)
                if parfunc and getattr(parfunc, '__doc__', None):
                    func.__doc__ = parfunc.__doc__
                    break
    return cls


class DocStringInheritor(type):
    """
    A variation on
    http://groups.google.com/group/comp.lang.python/msg/26f7b4fcb4d66c95
    by Paul McGuire
    """
    def __new__(meta, name, bases, clsdict):
        if not('__doc__' in clsdict and clsdict['__doc__']):
            for mro_cls in (mro_cls for base in bases for mro_cls in base.mro()):
                doc=mro_cls.__doc__
                if doc:
                    clsdict['__doc__']=doc
                    break
        for attr, attribute in clsdict.items():
            if not attribute.__doc__:
                for mro_cls in (mro_cls for base in bases for mro_cls in base.mro()
                                if hasattr(mro_cls, attr)):
                    doc=getattr(getattr(mro_cls,attr),'__doc__')
                    if doc:
                        attribute.__doc__=doc
                        break
        return type.__new__(meta, name, bases, clsdict)

class X(object):
    """
    some doc
    """
    #__metaclass__ = DocStringInheritor
    x = 20

    def please_implement(self):
        """
        I have a very thorough documentation
        :return:
        """
        raise NotImplementedError

    @property
    def speed(self):
        """
        Current speed in knots/hour.
        :return:
        """
        return 0

    @speed.setter
    def speed(self, value):
        """

        :param value:
        :return:
        """
        pass

@fix_docs
class SpecialX(X):

    def please_implement(self):
        return True

    @property
    def speed(self):
        return 10

    @speed.setter
    def speed(self, value):
        self.sp = value


class VerySpecial(X):
    p = 0
    """doc"""

    def please_implement(self):
        """

        :return bool: Always false.
        """
        return False

    def not_inherited(self):
        """
        Look at all these words!
        :return:
        """

help(X.speed)
help(SpecialX.speed)
help(SpecialX.please_implement)
help(VerySpecial.please_implement)
help(VerySpecial.not_inherited)

输出:

<function please_implement at 0x026E4AB0> needs doc
Help on property:

    Current speed in knots/hour.
    :return:

Help on property:


Help on method please_implement in module __main__:

please_implement(self) unbound __main__.SpecialX method
    I have a very thorough documentation
    :return:

Help on method please_implement in module __main__:

please_implement(self) unbound __main__.VerySpecial method
    :return bool: Always false.

Help on method not_inherited in module __main__:

not_inherited(self) unbound __main__.VerySpecial method
    Look at all these words!
    :return:

1 个答案:

答案 0 :(得分:1)

使用现在修复的DocStringInheritor元类,

Help on property:

    Current speed in knots/hour.
    :return:

Help on property:

    Current speed in knots/hour.
    :return:

Help on function please_implement in module __main__:

please_implement(self)
    I have a very thorough documentation
    :return:

Help on function please_implement in module __main__:

please_implement(self)
    :return bool: Always false.

Help on function not_inherited in module __main__:

not_inherited(self)
    Look at all these words!
    :return:

产量

//Collision with map tiles
function checkMove(px, py, pw, ph, pd) {
  console.time("checkMove execution")
  ... // Rest of function
  console.timeEnd("checkMove execution")
}