Python mutable type Vs immutable type

Source: Internet
Author: User

mutable type Vs immutable type (variable immutable here, refers to whether that piece of content in memory (value) can be changed. )

  • mutable type (mutable): list, dictionary
  • Immutable types (unmutable): Numbers, strings, tuples
  • In the case of an immutable type, when manipulating the object itself, a new area must be requested in memory (because the old region # is immutable #). The value of the variable is not allowed to change, if the value of the variable is changed, the equivalent of a new object, and for the same value of the object, in memory there is only one object, there is a reference count to record how many variables to refer to the object, when the reference calculation becomes 0, the garbage collection mechanism is enabled, The original address is no longer present.
  • In the case of mutable types, when manipulating objects, there is no need to apply memory elsewhere, where the address does not change and the value can change. For objects of the same value, there are different objects in memory, that is, each object has its own address, which is equivalent to how many copies of memory are stored for the same value object, there is no reference count, and it is a real object.
  • You can use the built-in function ID () to confirm whether an object's identity has changed before and after two assignments.

Python mutable type Vs immutable type

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.