描述符“getter”需要“属性”;对象但收到一个“函数”;

发布于 2024-12-03 20:03:34 字数 1215 浏览 1 评论 0原文

所以我在下面有一个 Table 对象的代码,它有一个字段名属性。

class Table(object):
    '''A CSV backed SQL table.'''
    @property
    def fieldnames(self):
        with open(self.filename) as f:
            return csv.DictReader(f).fieldnames

    @property.setter
    def fieldnames(self, fieldnames):
        with open(self.filename, 'w') as f:
            dr = csv.reader(f)
            dw = csv.DictWriter(f, fieldnames=fieldnames)
            dw.writerow(dict((field, field) for field in fieldnames))
            for row in self:
                dw.writerow(row)

当我尝试导入文件时,出现错误:

seas486:PennAppSuite ceasarbautista$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import table
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "table.py", line 7, in <module>
    class Table(object):
  File "table.py", line 9, in Table
    @property.getter
TypeError: descriptor 'getter' requires a 'property' object but received a 'function'

任何人都可以解释此错误的含义吗?

So I have this code below for a Table object, and it has a property for fieldnames.

class Table(object):
    '''A CSV backed SQL table.'''
    @property
    def fieldnames(self):
        with open(self.filename) as f:
            return csv.DictReader(f).fieldnames

    @property.setter
    def fieldnames(self, fieldnames):
        with open(self.filename, 'w') as f:
            dr = csv.reader(f)
            dw = csv.DictWriter(f, fieldnames=fieldnames)
            dw.writerow(dict((field, field) for field in fieldnames))
            for row in self:
                dw.writerow(row)

When I try to import the file, I get the error:

seas486:PennAppSuite ceasarbautista$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import table
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "table.py", line 7, in <module>
    class Table(object):
  File "table.py", line 9, in Table
    @property.getter
TypeError: descriptor 'getter' requires a 'property' object but received a 'function'

Can anybody explain what this error means?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-12-10 20:03:34

我猜它相当于 TypeError: unbound method ... must be called with ... instance as first argument (got ... instance instead)。要通过装饰器向属性添加setter,您必须使用.setter作为属性对象的成员/方法,而不是作为property的静态方法/类方法。代码应该如下所示:

class Table(object):
    '''A CSV backed SQL table.'''
    @property
    def fieldnames(self):
        with open(self.filename) as f:
            return csv.DictReader(f).fieldnames

    @fieldnames.setter # <<<
    def fieldnames(self, fieldnames):
        with open(self.filename, 'w') as f:
            dr = csv.reader(f)
            dw = csv.DictWriter(f, fieldnames=fieldnames)
            dw.writerow(dict((field, field) for field in fieldnames))
            for row in self:
                dw.writerow(row)

另请参阅文档中的示例。

I guess it's the equivalent to TypeError: unbound method ... must be called with ... instance as first argument (got ... instance instead). To add a setter to a property via a decorator, you have to use .setter as a member/method of the property object, not as a static method/classmethod of property. The code is supposed to look like this:

class Table(object):
    '''A CSV backed SQL table.'''
    @property
    def fieldnames(self):
        with open(self.filename) as f:
            return csv.DictReader(f).fieldnames

    @fieldnames.setter # <<<
    def fieldnames(self, fieldnames):
        with open(self.filename, 'w') as f:
            dr = csv.reader(f)
            dw = csv.DictWriter(f, fieldnames=fieldnames)
            dw.writerow(dict((field, field) for field in fieldnames))
            for row in self:
                dw.writerow(row)

Also see the example in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文