Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:58:37+00:00 2026-06-05T01:58:37+00:00

This is an attempt to better understand how reference count works in Python. Let’s

  • 0

This is an attempt to better understand how reference count works in Python.

Let’s create a class and instantiate it. The instance’s reference count would be 1 (getrefcount displays 2 because it’s own internal structures reference that class instance increasing reference count by 1):

>>> from sys import getrefcount as grc
>>> class A():
    def __init__(self):
        self.x = 100000


>>> a = A()
>>> grc(a)
2

a‘s internal variable x has 2 references:

>>> grc(a.x)
3

I expected it to be referenced by a and by A‘s __init__ method. Then I decided to check.

So I created a temporary variable b in the __main__ namespace just to be able to access the variable x. It increased the ref-number by 1 for it to become 3 (as expected):

>>> b = a.x
>>> grc(a.x)
4

Then I deleted the class instance and the ref count decreased by 1:

>>> del a
>>> grc(b)
3

So now there are 2 references: one is by b and one is by A (as I expected).

By deleting A from __main__ namespace I expect the count to decrease by 1 again.

>>> del A
>>> grc(b)
3

But it doesn’t happen. There is no class A or its instances that may reference 100000, but still it’s referenced by something other than b in __main__ namespace.

So, my question is, what is 100000 referenced by apart from b?


BrenBarn suggested that I should use object() instead of a number which may be stored somewhere internally.

>>> class A():
    def __init__(self):
        self.x = object()


>>> a = A()
>>> b = a.x
>>> grc(a.x)
3
>>> del a
>>> grc(b)
2

After deleting the instance a there were only one reference by b which is very logical.

The only thing that is left to be understood is why it’s not that way with number 100000.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:58:39+00:00Added an answer on June 5, 2026 at 1:58 am

    a.x is the integer 10000. This constant is referenced by the code object corresponding to the __init__() method of A. Code objects always include references to all literal constants in the code:

    >>> def f(): return 10000
    >>> f.__code__.co_consts
    (None, 10000)
    

    The line

    del A
    

    only deletes the name A and decreases the reference count of A. In Python 3.x (but not in 2.x), classes often include some cyclic references, and hence are only garbage collected when you explicitly run the garbage collector. And indeed, using

    import gc
    gc.collect()
    

    after del A does lead to the reduction of the reference count of b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt to create a GUI in MATLAB. I haven't been
In an attempt to better understand tables for future use, I was hoping somebody
Back Story : In an attempt to better understand Haskell and functional programming, I've
Why does this attempt at creating a list of curried functions not work? def
I used this snippet to attempt full screen in Windows, and this is what
I'm using this code to attempt to convert a float to an int, then
Alright so I've been making an attempt at this for a while now. My
This is my first attempt at C++, following an example to calculate a tip
This is my first attempt at using Github's shared collaboration model to work on
this is my first attempt at Stack overflow, so hopefully you all can help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.