简体   繁体   English

Python 动态添加属性总是返回相同的结果

[英]Python dynamically add property always returns same result

I'm relatively new to python.我对 python 比较陌生。 I created this minimal sample to demonstrate my problem.我创建了这个最小的示例来演示我的问题。 Within a loop I add some properties to my class during runtime.在一个循环中,我在运行时向我的 class 添加了一些属性。 If I query the properties later I always get the same result.如果我稍后查询属性,我总是得到相同的结果。 I printed and checked the function instance variables of the function and the property and they all differ as expected.我打印并检查了 function 的 function 实例变量和属性,它们都与预期的不同。 When I invoke the properties I always end up in the result of the last property.当我调用属性时,我总是以最后一个属性的结果结束。

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)

Results in:结果是:

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

Edit: I want the property to call the getter with the parameter MIN, MAX, DEF编辑:我希望该属性使用参数 MIN、MAX、DEF 调用 getter

With the hint from David Meu I modified the code and now it works:在 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM