Objects in Python

Source: Internet
Author: User

In Python everything is an object, each object has three attributes, (ID) identity, which is the address in memory, type, is int, character, dictionary (DIC), list, etc., value,

For example, a = 12 is an integer with 12, the value is 12, a space is created in the memory space (the space is represented by an address, that is, the ID), when the object is created, if the object's

The value can be changed, then called a mutable object (mutable), and if the value cannot be changed, it is called an immutable object (inmutable), and if an object contains references to other objects, it is called

As a container or a collection. For example, I created a dictionary type object and was b reference B = list () I created a dictionary type object and was c = c = dic () at this point B applies the dictionary type

Object b[' a '] = C then b at this point is a collection.

#is用来比较两个对象的id是不是相同的, test your identity.
If
is B: return and
#比较值
If a = = B:
Return a==
B
#比较类型
If Type (a) = = Type (b):
return type is the same
reference counting and garbage collection, all objects have reference counts. A= 22#creates an object with a value of 22 and is referenced by aB=a#the reference count for object 22 increasesc =[]c.append (b)#increased reference count for object 22actions to reduce reference countsdelA#Reduce reference countB = 56#Reduce reference countC[0] =33#Reduce reference countGet reference count>>>a = 22>>>ImportSYS>>>Sys.getrefcount (a)7In most cases, the reference count will be much more than we thought because the immutable objects (short strings and small values) are cached, and Pyth will share objects in different parts of the program to conserve memory

Not finished!

Objects in Python

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.