【发布时间】:2017-05-09 19:05:40
【问题描述】:
是否可以在变量中存储@property 函数?
假设我们有以下代码:
class I:
def __init__(self):
self.i = 0
def get_i(self):
return self.i
a = I()
f_i = a.get_i
print(f_i())
a.i = 3
print(f_i())
我将函数 get_i 保存在一个变量中并使用它。输出如预期:
0
3
现在让我们看一下相同的代码,但这次使用@property 函数:
class I:
def __init__(self):
self.i = 0
@property
def get_i(self):
return self.i
a = I()
f_i = a.get_i
现在a.get_i 不再是函数(它的值是 0)。有没有办法将函数仍然存储在 f_i 变量中?
【问题讨论】:
标签: python python-3.x properties