Python 未调用属性fset函数

Python 未调用属性fset函数,python,python-2.7,Python,Python 2.7,我不知道我在这里做错了什么。我总是使用property(fget=fget,fset=fset)初始化托管类属性,并且我有一个内存,我的getter和setter都在Python 2和3中使用。现在,直接使用代码,我发现我的getter可以工作,但是我的setter函数在Python2.x中似乎被忽略了-对属性的赋值只是抹去了它的属性,并用一个普通属性替换它。然而,在Python3.x中一切都很好。我做错了什么 class foo: def getcode( self ): return

我不知道我在这里做错了什么。我总是使用
property(fget=fget,fset=fset)
初始化托管类属性,并且我有一个内存,我的getter和setter都在Python 2和3中使用。现在,直接使用代码,我发现我的getter可以工作,但是我的setter函数在Python2.x中似乎被忽略了-对属性的赋值只是抹去了它的属性,并用一个普通属性替换它。然而,在Python3.x中一切都很好。我做错了什么

class foo:
    def getcode( self ): return self.__code
    def setcode( self, value ): self.__code = value.upper()[ : 1 ]
    def delcode( self ): raise AttributeError( "cannot remove the 'code' property" )
    code = property( fget=getcode, fset=setcode, fdel=delcode, doc='A single upper-case character' )

    def __init__( self, code='A' ): self.setcode( code )

f = foo(); f.code = 'A'
print( f.code )         # expect 'A', get 'A'
f = foo(); f.code = 'bbb'
print( f.code )         # expect 'B' but python 2.7.10 prints 'bbb'
f = foo(); f.code = 5   # expect an exception but python 2.7.10 does not raise one 
print( f.code )  

默认情况下,Python2.x类不从
对象继承,这使得
介绍了旧式课程和新式课程的区别
通过Python>2.2

property()
是一种简洁的方法,用于构建数据描述符,在访问属性时触发函数调用,但它完全可以作为 仅在新样式类的文档中描述:类具有
要从
对象继承

您的类不会从
对象继承
*
facepalm
*
。非常感谢。这也解释了为什么它总是起作用:我过去知道这是至关重要的。如果你把你的评论推广到一个答案上,我会感到荣幸地接受它。