Python Variable Object reference

Source: Internet
Author: User

1. Variables

Variables are created the first time they are assigned, they must be assigned before they are used

The variable itself has no type, and the variable type is the object type it refers to;

The variable is replaced with the object it refers to when it is used

2. Objects

The object itself has a count and type, the variable refers to the object, and when the object's reference becomes 0, the object memory is reclaimed. But a small type of object, such as int, does not necessarily reclaim its memory immediately.

Modifying any of the variables that point to the same Mutable object will affect the object that is pointed to, that is, the values of the two variables will be modified.

>>> l=[1,2,9]>>> s= l>>> s[0]=11>>> s[, 2, 9] >>> l[, 2, 9]>>>

A copy between mutable objects:

1. Copy.copy a shallow copy copies only the parent object and does not copy the inner sub-objects of the object.
2. copy.deepcopy deep copy copy objects and their sub-objects

3. Can be used to determine whether 2 objects are the same

ImportCopya= [1, 2, 3, 4, ['a','b']]#Original Objectb= A#assignment, a reference to a passing objectc = Copy.copy (a)#object Copy, shallow copyD = Copy.deepcopy (a)#object Copy, deep copyA.append (5)#Modify Object AA[4].append ('C')#Modify the [' A ', ' B '] Array object in Object aPrint('a=', a)Print('b=', B)Print('c=', c)Print('d=', D)Print('A is B'+ F isb)Print('A is C'A isc)Print('D is a'D isA

Output Result:
a= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
b= [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
c= [1, 2, 3, 4, [' A ', ' B ', ' C ']]
d= [1, 2, 3, 4, [' A ', ' B ']]
A is B True
A is C False
D is a False

Python Variable Object reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.