【发布时间】:2018-03-13 04:31:15
【问题描述】:
使用 python 2.7,假设我有一个 Test 类,其新式类语法定义如下。
class Test(object):
def __init__(self):
self._a = 5
@property
def a(self):
return self._a
@a.setter
def a(self, val):
self._a = val
t = Test()
print t.a
t.a = 4
print t.a
print t._a
运行上面的代码将打印5,4,4,这是所需的行为。
但是,如果我将上面代码的第一行更改为class Test:,那么结果将变为5,4,5。
有谁知道导致这种输出差异的原因是什么?
【问题讨论】:
标签: python python-2.7 class new-style-class