Python object model

Source: Internet
Author: User

objects for Python
A Python object consists of two parts: a type identifier (Identity object type), a reference counter (required by the garbage collection mechanism)
Each object has two standard header information: A type identifier to identify the type of the object, and a reference counter to determine whether the object can be recycled;
relationship of objects and variables
1. Variables (essentially pointers) and objects are stored in different parts of memory respectively
2. Variables always point to objects, and never point to other variables, but larger objects may be connected to other objects;
3, within Python, as an optimization, Python pre-caches some immutable objects and reuse them;
For example: None object, none is a python definition to represent a null value, there is an address in memory to save it
categories of objects
mutable objects and immutable objects
A mutable object can also invoke a function change once it is created but the address does not change, that is, the variable points to the original object.
There are: List,dictionary,set
Immutable objects, in contrast, cannot be changed after they are created, and a new object is returned if the call changes the function.
There are: int,string,float,tuple
extra Knowledge: deep copy and shallow copy of objects
Shallow copy: Just point the reference to the same memory address,
Deep copy: Creates a new piece of memory to store the value of the copied object.
Note: Deep copy is a folder-like hierarchy of recursion, that is, every time the discovery is a reference, then deep copy
Code:
Shallow copy: A=b
Deep copy: A=copy.deepcopy (b)
Copy of the first layer: A=copy.copy (b), note that the Copy.copy function selects the copy operation based on the current Copy object type.
If the type is not mutable, it is converted to a shallow copy. If it is a mutable type, deep copy is made (but does not have a recursive effect)

Python object model

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.