为什么我的代码运行错误,它是关于'@property'

时间:2009-12-27 03:57:06

标签: python

我使用python 2.5,我想知道当Platform是python2.5或python2.6时如何更改下一个代码

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

a=C()
print a.x#error

感谢


谢谢,亚历克斯,我认为你的例子中的属性必须是3个参数

但是,我看过一个代码,其中'property'只使用了1个论证,为什么,它可以工作

class SortingMiddleware(object):
    def process_request(self, request):
        request.__class__.field = property(get_field)
        request.__class__.direction = property(get_direction)

1 个答案:

答案 0 :(得分:4)

Python 2.5不支持.setter的{​​{1}}和.deleter子装饰器;它们是在Python 2.6中引入的。

要处理这两个版本,您可以使用以下代码进行编码:

property
相关问题