Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Get The Current Reference Count of an Object in Python?

#1
How to Get The Current Reference Count of an Object in Python?

<div><p>The reference count is the number of times an object is referenced by a variable. If an object isn’t referenced anymore, it can be safely removed from the memory—what’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>
<h2>What’s the Current Reference Count of Python Object</h2>
<p><em><strong>Question</strong>: How to get the current reference count of a Python object?</em></p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<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’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>
<p>Here’s the general strategy for an arbitrary <code>object</code>:</p>
<pre class="wp-block-preformatted">import sys
<strong>print(sys.getrefcount(object))</strong></pre>
<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>
<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))
# 13 y = x print(sys.getrefcount(x))
# 14
</pre>
<p>You may not have expected that the initial reference count is 13. So, why is that? Let’s see.</p>
<h2>Why is the Reference Count Higher Than Expected?</h2>
<p><strong>Reason 1: Implicit Helper Variables</strong></p>
<p>Python is a high-level programming language. It provides you many convenience functions and memory management. You don’t have to care about the concrete memory locations of your objects and Python automatically performs garbage collection—that is—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’ve created. All of this happens under the hood, so even if you don’t refer an object in your code, Python may still have created many references to that object. That’s why the reference count can be higher than expected. </p>
<p><strong>Reason 2: Temporary Reference in </strong><code><strong>getrefcount()</strong></code></p>
<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>
<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>
</div>


https://www.sickgaming.net/blog/2020/12/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016