Python Learning--mutable and immutable

Source: Internet
Author: User

Python data types are divided into mutable (variable) and immutable (immutable)

Mutable:list, Dict

Inmutable:int, string, float, tuple ...

Mutable and immutable are literally understood to mean that data is mutable and data is immutable.

Since the python variable (variable) does not need to be declared, and at the time of assignment, the variable can be re-assigned to any value, which involves an important core concept of Python: the dynamic type (typing )

Here again and again, in Python, all objects, Python is a pure-blooded object-oriented programming language, unlike Java. We know that there are boxing and unboxing in Java, and that the basic data type int is encapsulated as an integer in the process of persistence.

But in Python, everything is the object. Everything is an object, including your code, the object is an entity stored in memory, we use the object name in the program, just a reference to the object (reference).

References and object separation are the core of dynamic types, and references can point to a new object at any time

This differs from the concept of pointers and references in C + +, where pointers are logically independent in C + +, but references are bound together with objects.

For example:

A = ' viease '

A = 2

The first statement establishes the object ' Viease '(String type) in memory and assigns a pointer to reference a to it

The second sentence is that 2 is an integer object in memory that references a to 2, so that ' viease ' has no reference to it, and Python automatically destroys the garbage that no reference points to, freeing the corresponding memory space

(and for smaller integers and short characters python caches these objects, rather than creating and destroying them frequently)

Immutable (immutable data type)

For example

def func (x):    x=1    Print (x) x=2func (x) print (x)

The output result is

1

2

This is similar to the value passing in C/E + +, that is, the passed reference cannot change itself, but only changes the reference point to the

##############################################################

def func (x):    x[0]=4    Print (x) x=[1]func (x) print (x)

The output result is

[4]

[4]

This is similar to the pointer passing in C + +, that is, the passed reference can refer to its own element to change itself, changing the value of the reference

However, tuple (tuple), although it is possible to invoke a reference element but cannot be assigned a value, cannot alter the object itself, so it is also considered immutable object.

So summarize:

For inmutable object we pass in function parameter is value pass

For mutable object, we pass in function arguments is pointer passing






Python Learning--mutable and immutable

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.