Pylint:使用 属性 装饰器的成员函数导致 "no-member" 错误

Pylint: Using property decorator's member function results in "no-member" error

我 运行 遇到了 Pylint 的以下问题:

给定以下最小示例:

#tpack/__init__.py
class C:
    @property
    def ans(self):
        return 42

def f(c):
    return C.ans.fget(c)

Pylint 产生以下错误:

>pylint -d missing-docstring -d invalid-name -d too-few-public-methods tpack
************* Module tpack
tpack\__init__.py:7:11: E1101: Method 'ans' has no 'fget' member (no-member)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Pylint 版本:

>pylint --version
pylint 2.1.1
astroid 2.0.4
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]

这是 Pylint 的已知问题吗?

编辑:

这段代码的使用好像有些混乱

这是一个例子:

>python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from tpack import *
>>> c = C()
>>> f(c)
42

当通过 class 访问 属性 时,获取装饰器生成的实际 属性 对象。通过这个可以访问属性对象的成员函数。 fgets 就是 getter。只需要将类型为 C 的对象传递给它,然后返回 属性。

恐怕 Pylint 就在这里,但消息有点误导。

因此您的 C class 有一个名为 ans 的 属性,并且 ans 定义接收 self 作为参数;稍后你用 C.ans 调用 C class 本身,但在 class 的上下文中,Python 应该作为 self 传递ans?

的参数

所以问题是 self 没有在 class 的上下文中设置,所以您只能从 C 的实例访问属性,而不能从 C class 本身。

好吧,所有的评论和答案都是有道理的,因为实例方法应该只在对象上调用,而不是在 classes 上调用。但是,这里因为方法 'ans' 使用了 @属性 装饰器,在 class returns 对象上调用 'ans' 一个 属性 对象。 所以,做

print(C.ans)

打印

<property object at 0x000001A819E55CC8>

c = C()
print(c.ans)

打印

42

所以你的代码是正确的。理想情况下可能不是 Pythonic。但是,我已经测试了你的代码,它运行得很好。

所以,是的,这可能是 Pylint 的问题。我对 Pylint 没有太多的了解。所以帮不了你:(