gpt4 book ai didi

Python继承文档字符串错误 'read only'

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:42 24 4
gpt4 key购买 nike

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

例如提出的解决方案here给我错误:

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

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

TypeError: Error when calling the metaclass bases
readonly attribute

我发现的关于文档字符串只读的唯一引用是 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:

最佳答案

使用 DocStringInheritor 的现已修复版本元类,

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:
if isinstance(attribute, property):
clsdict[attr] = property(attribute.fget, attribute.fset, attribute.fdel, doc)
else:
attribute.__doc__=doc
break
return type.__new__(meta, name, bases, clsdict)


class X(object):
__metaclass__ = DocStringInheritor

"""
some doc
"""
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

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)

产量

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:

关于Python继承文档字符串错误 'read only',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com