{"id":122317,"date":"2020-12-20T19:15:44","date_gmt":"2020-12-20T19:15:44","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=19079"},"modified":"2020-12-20T19:15:44","modified_gmt":"2020-12-20T19:15:44","slug":"how-to-get-the-current-reference-count-of-an-object-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/20\/how-to-get-the-current-reference-count-of-an-object-in-python\/","title":{"rendered":"How to Get The Current Reference Count of an Object in Python?"},"content":{"rendered":"<p>The reference count is the number of times an object is referenced by a variable. If an object isn&#8217;t referenced anymore, it can be safely removed from the memory&#8212;what&#8217;s the use of an object nobody cares for anyway? This article shows you how to count the number of references to a Python object. <\/p>\n<h2>What&#8217;s the Current Reference Count of Python Object<\/h2>\n<p><em><strong>Question<\/strong>: How to get the current reference count of a Python object?<\/em><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/12\/refcount-1024x576.jpg\" alt=\"Get Reference Count Object Python\" class=\"wp-image-19087\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/12\/refcount-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/12\/refcount-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/12\/refcount-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/12\/refcount-150x84.jpg 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>Answer<\/strong>: You can get the current reference count with the <code>sys<\/code> module&#8217;s <code>getrefcount()<\/code> method. For example, to get the number of times variable <code>x<\/code> is referenced, run <code>sys.getrefcount(x)<\/code>. <\/p>\n<p>Here&#8217;s the general strategy for an arbitrary <code>object<\/code>:<\/p>\n<pre class=\"wp-block-preformatted\">import sys\n<strong>print(sys.getrefcount(object))<\/strong><\/pre>\n<p>The following code snippet shows an example of an object <code>x<\/code> that has a reference count of 13. After creating a new reference y to the object <code>x<\/code>, the reference count increases by one to 14. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sys x = 42 print(sys.getrefcount(x))\n# 13 y = x print(sys.getrefcount(x))\n# 14\n<\/pre>\n<p>You may not have expected that the initial reference count is 13. So, why is that? Let&#8217;s see.<\/p>\n<h2>Why is the Reference Count Higher Than Expected?<\/h2>\n<p><strong>Reason 1: Implicit Helper Variables<\/strong><\/p>\n<p>Python is a high-level programming language. It provides you many convenience functions and memory management. You don&#8217;t have to care about the concrete memory locations of your objects and Python automatically performs garbage collection&#8212;that is&#8212;removing unused objects from memory. To do all of this work, Python relies on its own structures. Among those, are helper variables that may refer to an object you&#8217;ve created. All of this happens under the hood, so even if you don&#8217;t refer an object in your code, Python may still have created many references to that object. That&#8217;s why the reference count can be higher than expected. <\/p>\n<p><strong>Reason 2: Temporary Reference in <\/strong><code><strong>getrefcount()<\/strong><\/code><\/p>\n<p>Note that by calling the function <code>getrefcount(x<\/code>), you create an additional temporary reference to the object <code>x<\/code>. This alone increases the reference counter by one. <\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/how-to-get-the-current-reference-count-of-an-object-in-python\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Get The Current Reference Count of an Object in Python?<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The reference count is the number of times an object is referenced by a variable. If an object isn&#8217;t referenced anymore, it can be safely removed from the memory&#8212;what&#8217;s the use of an object nobody cares for anyway? This article shows you how to count the number of references to a Python object. What&#8217;s the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-122317","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=122317"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122317\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=122317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=122317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=122317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}