变量更改的价值

时间:2019-05-29 11:40:40

标签: python-3.x

首先,对不起标题,但是我不知道如何解释我的问题。一个例子更好,我有:

def myfunction(param):
    res = some_action_with_param
    return res

def other_function():
    res1 = myfunction(param="value1")
    res2 = myfunction(param="value2")

如果参数是“ value1”或“ value2”,则myfuncion返回的值是不同的,但是如果我打印res1和res2,则得到相同的值(res2的值):

    def other_function():
        res1 = myfunction(param="value1")  # imagine that is returned the string: "the result is 1"
        print(res1)
        res2 = myfunction(param="value2") # imagine that is returned the string: "the result is 2"
        print(res2)
        print(res1)

the output is:

    the result is 1
    the result is 2
    the result is 2

why the variable res1 change its value being the same value of res2?

thanks a lot.

[update]
thanks for answer. the code is:

def collect_data_compare(field_values, device):
        def xstr(s): return s or "none"  # convert a null or none to string
        values = field_values
        try:  # collect info from reference phone.
           phone = Phone(name=device)
           values['Device Name'] = phone.name 
        <----- cut ----->
            return values

def get_comparation(devRefUser,devOnboard): 
        fields = {'Device Name': '', 'Dir. Number': '', 'Device Type': '', 'Phone Button Template': '', 'Softkey Template': '', 'Owner user ID': '',
                  'End-User ID (Corp.)': '', 'External Phone Mask': '', 'Line Groups': '', 'Call Pick-Up Group': '', 'Cfwd All ext.': '', 'Cfwd Busy ext.': '', 'Cfwd No Answer ext.': ''}

        ref_values = collect_data_compare(
            field_values=fields, device=devRefUser)
        print(ref_values)
        new_values = collect_data_compare(
            field_values=fields, device=devOnboard)
        print(new_values)
        print(ref_values)

第一个print(ref_values)中的最后一个不等于最后一个。实际上,最后一个与print(new_values)

相同

0 个答案:

没有答案