Python mutable objects and immutable objects

Source: Internet
Author: User

An immutable object in which the object points to an in-memory value that cannot be changed. when a variable is changed, because the value it refers to cannot be changed, it is equivalent to copy the original value and then change, this will open a new address, the variable point to the new address.

A mutable object in which the object points to an in-memory value that can be changed. variables (accurately referred to) after the change, in fact, it refers to the value of the direct change, and there is no replication behavior, there is no new address, the popular point is to change in situ .

In Python, Int,float,string,tuple,bytes is an immutable object, and Set,list,dict is a mutable object.

Int:

1 >>> a =2 >>> ID (a)3 1402899362180484 >>> A = A + 15 >>> ID (a)6 140289936218024
View Code

Float

1 >>> a = 10.552 >>> ID (a)3 1402899362260244 >>> A = a + 1.55 >>> ID (a)6 140289936226000
View Code

String

1 ' ABC ' 2 >>> ID (a)3 43561351124'd'5 >>> ID (a)6 4357016240
View Code

Tuple:

1 >>> a = (1, 2, 3)2 >>> ID (a)3 43569135844 > >> A = (4, 5, 6)5 >>> ID (a)6 4356799536
View Code

bytes

1 >>> a = b'abc'2 >>> ID (a)3 43561351124'def'5 >>> ID (a)6 4357016288
View Code

Set

1 >>> a = set ([1, 2, 3])2 >>> ID (a)3 43567542244 >>> A.add (4)5 >>> ID (a)6 4356754224
View Code

List

1 >>> a = [1, 2, 3]2 >>> ID (a)3 43569056564 > >> A.append (4)5 >>> ID (a)6 4356905656
View Code

Dict

1 >>> a = {'one'and ' both ': 2}2 >>> ID (a)3 43570245764 >>> a['three '] = 35 >>> ID (a)6 4357024576
View Code

Python mutable objects and immutable objects

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.