Variables and garbage collection mechanism in Python

1. Variables in python

pythonAnd javathe variables in are essentially different.

javaWhen declaring a variable in the variable, you must specify the data type of the variable, int, str, or a certain type, and then the virtual machine will apply for a space in the memory. The size of the space is related to the type. The popular understanding is to imagine variables as a box, and what can be contained in the box is set at the beginning.

For example a=1, put 1 in the box.

pythonThe variable is essentially a pointer, the size of the pointer is the same. For example, a pointer to an int type, the size of the pointer itself is fixed, and there is no need to consider the memory size occupied by the int itself, but it is placed in memory anyway. You only need to find the pointer when accessing the int object.

Take a=1, first to declare an int type object in memory, to open up a space to store 1, then a 1 point.

a = [1,2,3]
b = a
b.append(4)
print(a) # [1,2,3,4]

Here, if you use the box idea to understand it: put the list in [1,2,3]one box, and put a in another box, then if you print a after modifying b, a will not change.

a = [1,2,3]
b = a
print(a is b) # True

It iscan also be seen that the asum bis the same object, that is a, the idvalue of the bpointed object idis the same as the value of the pointed object .

2. The difference between == and is

  • is

Finally, I said a = [1,2,3] b = athat a and b are the same object. So what if it is two assignments?

a = [1,2,3]
b = [1,2,3]
ptint(a is b) # False
print(id(a) == id(b)) #False

From the results, a and b at this time are different objects. In other words, an object will be re-declared when an assignment statement is used.

There is a special case as follows

a = 1
b = 1
print(a is b) #True

pythonInternal internmechanism-when encountering the same small integer within a certain range, no new object is generated, and it directly points to the original object. An internal optimization mechanism.

The same applies to small strings.

a = "abc"
b = "abc"
print(a is b) #True

Use is to judge the class

class People:
    pass
person = People()
# isinstance(person,People)
if type(person) is People:
    print('Yes') # Yes

Because the class itself is an object, and the object is a globally unique, personactually point to People, so type(person)and Peoplethe id is the same.

  • ==
a = [1,2,3]
b = [1,2,3]
ptint(a == b) # True

a is an object of type list. A magic function is implemented in list __eq__. When it encounters ==, it will call this function to determine whether the value of the object is equal.

3. The del statement and garbage collection mechanism

pythonThe garbage collection algorithm in is using reference counting.

First define a=1, b=aand then 1 will automatically generate a counter on this object, and the a=1hour counter will increase by 1, b=aindicating that b also points to a. At this time, the counter increases by 1, which is equivalent to 1 with two variables pointing to it.

When we del adelete the object, the counter will decrease by 1. When the counter decreases to 0, the python interpreter will reclaim the object (cannot be occupied in memory all the time).

The delete statement in c++ is to directly reclaim the object, which is different from python.

Give a chestnut

a=object()
b=a
del a
print(b) # object object at 0x0000000003D90F0
print(a) # name a is not defined

The above result is that b can be printed, but a cannot be printed. The process is to delete the object a and decrement the reference counter by 1.

When the pythoninterpreter reclaims the object, it will call the object's __del__magic function. So when we do garbage collection, we hope that certain resources will be released when the object is collected, which can be achieved by overloading __del__functions.

class A:
	def __del__(self):
		pass

4. Some notes about the incoming list

First case

def add(a,b):
    a+=b
    return a
if __name__ == "__main__":
    a = 1
    b = 2
    c=add(a,b)
    print(c) # 3
    print(a,b) # 1 2

Second case

def add(a,b):
    a+=b
    return a
if __name__ == "__main__":
    a = [1,2]
    b = [3,4]
    c=add(a,b)
    print(c) # [1,2,3,4]
    print(a,b) # [1,2,3,4] [3,4]

It is found that the input a has changed at this time, because the list is a variable type, and the += symbol is directly assigned to the original list variable a during operation, so the original a has changed.

The third case

def add(a,b):
    a+=b
    return a
if __name__ == "__main__":
    a = (1,2)
    b = (3,4)
    c=add(a,b)
    print(c) # (1,2,3,4)
    print(a,b) # (1,2) (3,4)

to sum up

When three different types of parameters are passed in, only the list object has an impact on the original data.

So when passing an object to a function, if the object is of a list、dicttype whose value can be modified, it may cause the original data to change.

Guess you like

Origin blog.csdn.net/weixin_43901214/article/details/107039987