Python's reference count Analysis (ii)

Source: Internet
Author: User

The case where all Python object reference counts are reduced by 1:

I. The alias of the object is assigned a new object;

A = 23345455  #  added a reference to B = a  #  added a reference to print= 1.4  #  reduced the reference to a 23345455 integer print(Sys.getrefcount (a))
Results: 3;2
Two. The alias of the object is explicitly destroyed;
A = 23345455  #  adds a reference to B = a  #  adds a reference to list = [A, b]  #  Added 2 references del  aprint(Sys.getrefcount (b))
Results: 4

Description: Use the DEL keyword or the Del () function directly; Note: The above code is manually destroyed by the assigned reference to a, but in the list of a will not be destroyed.

Three. An object leaves its scope;

A = 23345455  #  adds a reference to B = a  #  adds a reference to print(Sys.getrefcount (a))  #  after execution, reference to destroy print(Sys.getrefcount (a))

Results: 3;3

Note:A is passed as a parameter to the Sys.getrefcount (a) function, only works in the function and is destroyed once execution is complete.

Four. The container in which the object is located is destroyed, or the object is deleted from the container;

#  added a reference to a = 23345455#  adds a reference to B == [A, b]  #  added 2 references to del  listprint(Sys.getrefcount (b))

Results: 3

All reference counts plus one: python's reference count Analysis (i)

Python's reference count Analysis (ii)

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.