Home  >  Article  >  Backend Development  >  The difference between python variable types and immutable types

The difference between python variable types and immutable types

(*-*)浩
(*-*)浩Original
2019-06-25 15:36:095250browse

Mutable type Vs Immutable type

The difference between python variable types and immutable types

Variable type (mutable): list, dictionary (recommended learning: Python video tutorial)

Immutable types (unmutable): numbers, strings, tuples

The mutable and immutable here refers to whether the content (value) in the memory can be changed.

You can use the built-in function id() to confirm whether the identity of the object has changed before and after two assignments.

Variable data type, the variable name stores an address, the address points to a specific object, and no matter what operations are performed on the value of the variable, that is, the object, the variable name will not be changed. The stored address.

For variable types, take list as an example. After append, list still points to the same memory address, because list is a variable type and can be modified in place. ,

>>> a = [1, 2, 3]
>>> id(a)
>>> a.append(4)
>>> id(a)

Once the object of the immutable data type changes, a new space will be opened in the memory to store the new object, and the original variable name will point to a new the address of.

>>> i = 5
>>> i += 1
>>> i
>>> id(i)
>>> i += 1
>>> i
>>> id(i)

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between python variable types and immutable types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn