Reference, copy, function parameter transfer, instance call class variable of python knowledge point 1

1. Quote 

Details between variables and objects in python. (Or the separation of references and objects) 
In python, if you want to use a variable, you don't need to declare it in advance, you only need to assign a value to the variable when you use it. 
Example 1: 
a=1 
This is a simple assignment statement, where the integer 1 is an object and a is a reference. Using the assignment statement, the reference a points to object 1. You can view the memory address of an object through python's built-in function id(). 
Example 2:

a=2
print id(a)    #24834392
a='banana'
print id(a)    #139990659655312
  • 1
  • 2
  • 3
  • 4

In the first statement, 2 is an integer object stored in memory, and the reference a points to object 1 through assignment; in 
the second statement, a string object 'banana' is created in the memory, and the reference a points to the object 1 through assignment 'banana', and at the same time, object 1 no longer has a reference to it, it will be garbage collected by python's memory handling mechanism, freeing the memory. 
Example 3:

a=3
print id(a)    #10289448
b=3
print id(b)    #10289448
  • 1
  • 2
  • 3
  • 4

You can see that these two references point to the same object, why is this? This is related to python's memory mechanism, because for the language, frequent object destruction and creation is a waste of performance. So in Python, integers and short characters, Python caches these objects for reuse. 
(For details, please refer to the garbage collection mechanism of python knowledge points) 
Example 4:

a=4
print id(a#36151568
b=a    #引用b指向引用a指向的那个对象
print id(b)   #36151568
a=a+2
print id(a#36151520
print id(b)   #36151568
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The third sentence reassigns a to point to a new object 6. Although the reference of a has changed, the reference of b has not changed, and a and b point to different objects. 
It can be obtained that even if multiple references point to the same object, if the value of a reference changes, then the reference is actually made to point to a new reference and does not affect the pointing of other references. From the effect point of view, each reference is independent of each other and does not affect each other.

Example 5: 
References are further divided into points to mutable objects (such as lists) and points to immutable objects (numbers, strings, tuples).

L1=[1,2,3]
L2=L1
print id(L1)  #1396430512
print id(L2)  #1396430512
L1(0)=10
print id(L1)  #1396430512
print id(L2)  #1396430512
print L2  #[10,2,3]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

The same as Example 4, the value of one of the objects is modified, but it can be found that the results are not the same. The references to L1 and L2 have not changed, but the value of the list object [1,2,3] has become [10,2,3] (the list object has changed) 
In this case, we no longer have this reference to L1 A reference assignment , but an assignment to an element of the table pointed to by L1 . As a result, L2 also changes at the same time.

Comparative example 4 and example 5 can be obtained, refer to the immutable object, can not change the object itself, only change the reference point. When referring to a mutable object, assignment operations can directly change the referenced object itself (that is, modify the value of the object).

A list can change the object itself (in-place change) by referencing its elements. This type of object is called a mutable object, and the dictionary is also such a data type. Like the previous numbers and strings, the object itself cannot be changed, only the reference point can be changed, which is called an immutable object.

To judge whether the objects pointed to by two references are the same, the is keyword is available: 
is is judged by comparing the memory address (id), and returns True or False.

(Extension: python objects have three elements: id, type, value, 
is judged by id, == judged by value)

2. Copy

Shallow copy (copy) : Copy an object, but the properties of the object still refer to the original, that is, add a pointer to the existing memory.  
Assuming the original object "will", since a shallow copy of "will" will create a new object "willber", the id of "will" is different from that of "willber". (ie "wilber is not will") However, for the elements in the object, the shallow copy will only use the reference (memory address) of the original element, that is, "wilber[i] is will[i]". See Example 6 for how the modified elements of the original object are reflected on the copied object.

Deep copy (deepcopy) : increase a pointer and allocate a new memory, so that the increased pointer points to the new memory.

Example 6:

import copy
a=[1,2,3,4,['a','b']] #原始对象

b=a  #赋值,传对象的引用
c=copy.cpoy(a) #浅拷贝
d=cpy.deepcopy(a)  #深拷贝

a.append(5) #修改对象a
a[4].append('c')   #修改对象中的['a','b']数组对象

print a   #[1,2,3,4,['a','b','c'],5]
print b   #[1,2,3,4,['a','b','c'],5]
print c   #[1,2,3,4,['a','b','c']]
print d   #[1,2,3,4,['a','b']]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

First of all, a is a reference to a mutable object, so modify a, the value of the mutable object will also change, a and b point to the same object, so the values ​​of a and b are the same. 
Secondly, c is a shallow copy of a, and the overall change of each element of the shallow copy has no effect. But only the element part modification is mutually restrained. If the modified element is an immutable type, such as a[0], the first element of the list corresponding to a will use a new object, and c still points to the original object; if the modified element is an immutable type, such as a [4], the modification operation will not generate a new object, so the modification result of a will be reflected on c accordingly. 
Finally, d is a deep copy, there is new memory to store the original object, so it doesn't change.

3. The use of reference problems in functions, classes and instances:

3.1 Parameter passing of functions

a = 1
def fun(a):
    a = 2
fun(a)
print a
  • 1
  • 2
  • 3
  • 4
  • 5
a = []
def fun(a):
    a.append(1)
fun(a)
print a  # [1]
  • 1
  • 2
  • 3
  • 4
  • 5

When a reference is passed to a function, the function automatically copies a reference, and the reference in the function has no relationship with the reference outside. 
So in the first example, the function refers to an immutable object, and when the function returns, the outside reference doesn't feel anything. 
The second example is different. The reference in the function points to a variable object, and the operation on it is the same as locating the pointer address and modifying it in memory.

3.2.实例调用类变量

class Person:
    name="aaa"

p1=Person()
p2=Person()
p1.name="bbb"
print p1.name  # bbb
print p2.name  # aaa
print Person.name  # aaa
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

类变量就是供类使用的变量,实例变量就是供实例使用的. 
这里p1.name=”bbb”是实例调用了类变量,这其实和上一个问题一样,就是函数传参的问题,p1.name一开始是指向的类变量name=”aaa”,但是在实例的作用域里把类变量的引用改变了,就变成了一个实例变量,self.name不再引用Person的类变量name了.

class Person:
    name=[]

p1=Person()
p2=Person()
p1.name.append(1)
print p1.name  # [1]
print p2.name  # [1]
print Person.name  # [1]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

引用部分参考网址https://www.cnblogs.com/ShaunChen/p/5656971.html 
参考http://blog.csdn.net/u013510614/article/details/50751017

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645097&siteId=291194637