如果 属性 样本工作

if a property sample work

在"Core Python Programming"的书中,有一个关于如何使用属性的示例。代码是这样的:

class Hidex(object):
    def __init__(self, x):
        self.__x = x
    @property
    def x():
        def fget(self):
            return ~self.__x
        def fset(self, x):
            assert isinstance(val, int), 'val must be int'
            self.__x = ~x
        return locals()

书上说,这个 class 可以用下面的代码工作:

inst = Hidex(20)
print inst.x
inst.x = 30
print inst.x

但我认为 class 行不通。因为当访问 inst.x 时,解释器实际上会 运行 Hidex.__dict__['x'].__get__(x, Hidex),并且因为 x = 属性(x),属性 的第一个参数 'fget'是x,而不是x().

中定义的函数'fget'

此外,当我 运行 这段代码时,我得到的结果是:

{'fget': <function fset at 0x.....>, 'self': <__main__.xxxx>, 'fget': <function fget at 0x....>}
traceback:
...... # this result is just telling t.x = 30 cannot run, just skip the details
AttributeError: cannot set attribute

我错过了什么吗?为什么这本书打算这样行得通?

这是有道理的:

class Hidex(object):

    def __init__(self, x):
        self.__x = x

    @property
    def x(self):
            return ~self.__x

    @x.setter
    def x(self, x):
        assert isinstance(x, int), 'val must be int'
        self.__x = ~x

看起来你问题的代码中的 @property 不是内置的,而是不同的版本。

这可能是此处的意图:

def nested_property(func):
    """Make defining properties simpler.
    """
    names = func()
    names['doc'] = func.__doc__
    return property(**names)


class Hidex(object):
    def __init__(self, x):
        self.__x = x
    @nested_property
    def x():
        def fget(self):
            return ~self.__x
        def fset(self, x):
            assert isinstance(x, int), 'val must be int'
            self.__x = ~x
        return locals()