Python属性未设置

ton*_*ony 6 python properties

这是代码:

def Property(func):
     return property(**func())

class A:
     def __init__(self, name):
          self._name = name

     @Property
     def name():
          doc = 'A''s name'

          def fget(self):
               return self._name

          def fset(self, val):
               self._name = val

          fdel = None

          print locals()
          return locals()

a = A('John')
print a.name
print a._name
a.name = 'Bob'
print a.name
print a._name
Run Code Online (Sandbox Code Playgroud)

以上产生以下输出:

{'doc': 'As name', 'fset': <function fset at 0x10b68e578>, 'fdel': None, 'fget': <function fget at 0x10b68ec08>}
John
John
Bob
John
Run Code Online (Sandbox Code Playgroud)

代码取自这里.

问题:怎么了?它应该是简单的东西,但我找不到它.

注意:我需要属性来进行复杂的获取/设置,而不是简单地隐藏属性.

提前致谢.

voi*_*hos 8

各州的文件property():

返回新样式类的属性属性(从对象派生的类).

您的类不是新样式的类(您没有从对象继承).将类声明更改为:

class A(object):
    ...
Run Code Online (Sandbox Code Playgroud)

它应该按预期工作.