简体   繁体   中英

python mutable and immutable function arguments

I stumbled over a "strange behaviour"(as in strange for me) of python. I thought i created a list "a" trough passing the first element of ParamList, which is a list. Then i changed "a" in another function, but than also ParamList[0] also got changed. I didn't expect that.

Look at the following minimal code

def f(ParamList):
    b = ParamList[1]
    a = ParamList[0]
    print('id(ParamList[0]), id(a)', id(ParamList[0]), id(a))
    a, b = Problemmaker(a,b)
    print('a, b, ParamList', a, b, ParamList)

def Problemmaker(a,b):
    a[2]=99
    print('id(a)', id(a))
    b = 55
    return a,b

if __name__ == "__main__":
    Initial = [[2,3,4],2]

    f(Initial)

i already found out what, the problem is.

So apparently what i did is, that "a" and ParamList[0] get referenced to the same adress.

So i was thinking ok, "a" gets the value of ParamList[0] but in fact, all i did was referencing "a" to the same adress. So when i changed "a", the value that was stored in that adress also got changed and so got ParamList[0].

So if i wan't to change "a" but not ParamList[0], i can create a new List for "a" and the problem is avoided.

def f(ParamList):
    b = ParamList[1]
    a = [*ParamList[0]]
    print('id(ParamList[0]), id(a)', id(ParamList[0]), id(a))
    a, b = Problemmaker(a,b)
    print('a, b, ParamList', a, b, ParamList)

def Problemmaker(a,b):
    a[2]=99
    print('id(ParamList[0]), id(a)', id(a))

    b = 55
    return a,b

if __name__ == "__main__":
    Initial = [[2,3,4],2]

    f(Initial)

This is emberassing, because i code in python for a long time know and i was always thinking that i know what's what, but yeah admitting this mistake hopefully makes me more meticulous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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