python assignment operation, shallow copy, deep copy and detailed analysis of function transfer parameters

Students who have studied C language can distinguish between variables and pointers, while python variables are all in the form of pointers, so when assigning variables (such as a=b), they only copy the index. If you want to copy the contents of the pointer index, you need to use light copy and deep copy. Let's introduce the assignment operation of python and the difference between shallow copy and deep copy.

Assignment operation

If we assign variable b to variable a, in essence, we point variable b to the object that variable a points to. As shown in the figure below:

Therefore, when we change the content of the object, no matter a or b, the content has changed.

import copy
class aclass(object):
    def __init__(self):
        self.list = [1, 2, [0, 0]]
        self.variable = 3

def main():
    a = aclass()
    b = a
    print(id(a), id(b), a is b)
    b.variable = 0
    print(a.variable)

if __name__ == '__main__':
    main()

The implementation results are:

Shallow copy

The result of shallow copy operation is to copy only the directly indexed objects, and not the internal child objects of the objects. As shown in the figure below:

After shallow copy, if we change variable b, the value of the direct object of variable a will not be changed, and the value of the child object will be changed.

import copy
class aclass(object):
    def __init__(self):
        self.list = [1, 2, ['a', 'b']]
        self.variable = 3

def main():
    a = aclass()
    b = copy.copy(a)
    print(id(a), id(b), a is b)
    b.variable = 0
    print(a.variable)
    b.list[0] = 0
    print(a.list[0])
    b.list[2][0] = 'c'
    print(a.list[2][0])

if __name__ == '__main__':
    main()

The implementation results are:

Deep copy

The result of a deep copy operation is to copy the object and its children. As shown in the figure below:

After deep copy, if we change variable b, variable a is not affected.

import copy
class aclass(object):
    def __init__(self):
        self.list = [1, 2, ['a', 'b']]
        self.variable = 3

def main():
    a = aclass()
    b = copy.deepcopy(a)
    print(id(a), id(b), a is b)
    b.variable = 0
    print(a.variable)
    b.list[0] = 0
    print(a.list[0])
    b.list[2][0] = 'c'
    print(a.list[2][0])

if __name__ == '__main__':
    main()

The implementation results are:

Which copy method does the function transfer parameters belong to?

Let's start with the conclusion: the parameter transfer of a function belongs to the way of assignment.
It can be verified by a simple code:

import copy
class aclass(object):
    def __init__(self):
        self.list = [1, 2, ['a', 'b']]
        self.variable = 3

def fun(b):
    print(id(b))
    b.variable = 0

def main():
    a = aclass()
    print(id(a))
    fun(a)
    print(a.variable)

if __name__ == '__main__':
    main()

The implementation results are:

Tags: Python Programming

Posted by carnold on Tue, 24 May 2022 19:54:06 +0300