Python 动态添加 属性 总是 returns 相同的结果
Python dynamically add property always returns same result
我对 python 比较陌生。我创建了这个最小示例来演示我的问题。在一个循环中,我在运行时向 class 添加了一些属性。如果我稍后查询属性,我总是会得到相同的结果。我打印并检查了函数和 属性 的函数实例变量,它们都与预期的不同。当我调用属性时,我总是以最后一个 属性.
的结果结束
class T:
def __init__(self):
specialList = ['MIN', 'DEF', 'MAX']
for special in specialList:
def get_property_special(self):
print('return get_current('+special+')') #call getter with special parameter
return special # just fake here. Should return value of getter
print("Dyn function", get_property_special)
specialProp = property()
print("Property "+ str(specialProp))
specialProp = specialProp.getter(get_property_special)
setattr(T, "current_" + special.lower(), specialProp)
print ("define property: current_" + special.lower())
b = T()
print(b.current_max)
print(b.current_min)
print(b.current_def)
结果:
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8805E0>
Property <property object at 0x0000026D6D8929A0>
define property: current_min
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8A9790>
Property <property object at 0x0000026D6D892AE0>
define property: current_def
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8A94C0>
Property <property object at 0x0000026D6D892B30>
define property: current_max
get_current(MAX) #ok
MAX
get_current(MAX) #Error: why MAX called min
MAX
get_current(MAX) #Error: why MAX called def
MAX
编辑:我希望 属性 使用参数 MIN、MAX、DEF
调用 getter
根据 David Meu 的提示,我修改了代码,现在可以运行了:
class T:
def __init__(self):
specialList = ['MIN', 'DEF', 'MAX']
for special in specialList:
def get_property_special(self, sp = special):
print('return get_current('+sp+')') #call getter with special parameter
return sp # just fake here. Should return value of getter
print("Dyn function", get_property_special)
specialProp = property()
print("Property "+ str(specialProp))
specialProp = specialProp.getter(get_property_special)
setattr(T, "current_" + special.lower(), specialProp)
print ("define property: current_" + special.lower())
b = T()
print(b.current_max)
print(b.current_min)
print(b.current_def)
我对 python 比较陌生。我创建了这个最小示例来演示我的问题。在一个循环中,我在运行时向 class 添加了一些属性。如果我稍后查询属性,我总是会得到相同的结果。我打印并检查了函数和 属性 的函数实例变量,它们都与预期的不同。当我调用属性时,我总是以最后一个 属性.
的结果结束class T:
def __init__(self):
specialList = ['MIN', 'DEF', 'MAX']
for special in specialList:
def get_property_special(self):
print('return get_current('+special+')') #call getter with special parameter
return special # just fake here. Should return value of getter
print("Dyn function", get_property_special)
specialProp = property()
print("Property "+ str(specialProp))
specialProp = specialProp.getter(get_property_special)
setattr(T, "current_" + special.lower(), specialProp)
print ("define property: current_" + special.lower())
b = T()
print(b.current_max)
print(b.current_min)
print(b.current_def)
结果:
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8805E0>
Property <property object at 0x0000026D6D8929A0>
define property: current_min
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8A9790>
Property <property object at 0x0000026D6D892AE0>
define property: current_def
Dyn function <function T.__init__.<locals>.get_property_special at 0x0000026D6D8A94C0>
Property <property object at 0x0000026D6D892B30>
define property: current_max
get_current(MAX) #ok
MAX
get_current(MAX) #Error: why MAX called min
MAX
get_current(MAX) #Error: why MAX called def
MAX
编辑:我希望 属性 使用参数 MIN、MAX、DEF
调用 getter根据 David Meu 的提示,我修改了代码,现在可以运行了:
class T:
def __init__(self):
specialList = ['MIN', 'DEF', 'MAX']
for special in specialList:
def get_property_special(self, sp = special):
print('return get_current('+sp+')') #call getter with special parameter
return sp # just fake here. Should return value of getter
print("Dyn function", get_property_special)
specialProp = property()
print("Property "+ str(specialProp))
specialProp = specialProp.getter(get_property_special)
setattr(T, "current_" + special.lower(), specialProp)
print ("define property: current_" + special.lower())
b = T()
print(b.current_max)
print(b.current_min)
print(b.current_def)