[Pyhton] weakref weak reference

Source: Internet
Author: User

[Pyhton] weakref weak reference

 

Interpretation in wiki: in computer programming, weak references, compared with strong references, mean that the referenced objects cannot be referenced by the garbage collector. If an object is referenced by a weak reference, it is considered inaccessible (or weak accessible) and may be recycled at any time. The main function is to reduce the number of unnecessary objects in memory.

For more details, see Wikipedia.

 

Easy to understand:

The garbage collection mechanism in python is to reference counters. When the reference number of an object is 0, it will be recycled from the memory. Garbage collection becomes unreliable when circular references occur.

 

Compare by a piece of code:

 

#-*-Encoding = UTF-8-*-''' author: orangleliupython small experiment with weak references ''' import weakrefimport gcclass NewObj (object): def my_method (self ): print called me obj = NewObj () r = weakref. ref (obj) gc. collect () print r () is objobj = 1gc. collect () # print r () is None, r () print ******************** 'obj = NewObj () s = objgc. collect () print s is objobj = 1gc. collect () print s is None, s

Comparison result:

 

True
True None
*******************
True
False <__ main _. NewObj object at 0x024FB870>

 

It is easy to see that the weak reference counter does not increase, so when obj does not reference NewObj, The NewObj object is released, so the reference object of r is gone. The s object is the same as the referenced object we usually use. When s = obj, the reference counter is + 1. So when obj is not pointed to NewObj, s still points to NewObj, the reference count for NewObj is 2-1 = 1.

 

Through the experiment we can see the principle, can this weak reference in what specific circumstances to use it, how to avoid the circular reference, you can refer to the case of pymeng W: http://pymotw.com/2/weakref/

This problem has not been solved in actual development, and will be used for further research.

 

 

This article from the "orangleliu notebook" blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40718121

Author: orangleliu


 

Related Article

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.