python: unintentionally modifying parameters passed into a function

前端 未结 4 828
死守一世寂寞
死守一世寂寞 2021-02-04 10:05

A few times I accidentally modified the input to a function. Since Python has no constant references, I\'m wondering what coding techniques might help me avoid making this mista

4条回答
  •  生来不讨喜
    2021-02-04 10:48

    Making copies of parameters 'just-in-case' is a bad idea: you end up paying for it in lousy performance; or you have to keep track of the sizes of your arguments instead.

    Better to get a good understanding of objects and names and how Python deals with them. A good start being this article.

    The importart point being that

    def modi_list(alist):
        alist.append(4)
    
    some_list = [1, 2, 3]
    modi_list(some_list)
    print(some_list)
    

    has exactly the same affect as

    some_list = [1, 2, 3]
    same_list = some_list
    same_list.append(4)
    print(some_list)
    

    because in the function call no copying of arguments is taking place, no creating pointers is taking place... what is taking place is Python saying alist = some_list and then executing the code in the function modi_list(). In other words, Python is binding (or assigning) another name to the same object.

    Finally, when you do have a function that is going to modify its arguments, and you don't want those changes visible outside the function, you can usually just do a shallow copy:

    def dont_modi_list(alist):
        alist = alist[:]  # make a shallow copy
        alist.append(4)
    

    Now some_list and alist are two different list objects that happen to contain the same objects -- so if you are just messing with the list object (inserting, deleting, rearranging) then you are fine, buf if you are going to go even deeper and cause modifications to the objects in the list then you will need to do a deepcopy(). But it's up to you to keep track of such things and code appropriately.

提交回复
热议问题