counting references to an instance

B

Brian

Hello;

Is it possible to get an object to return the number of references there
are to itself?

Ex:

class test(object):
pass

a = test()
b = a

# Should print "2", if I knew the name of the method.
print a.refCount()

Many thanks,

Brian.
 
T

Thomas Heller

Brian said:
Hello;

Is it possible to get an object to return the number of references there
are to itself?

Ex:

class test(object):
pass

a = test()
b = a

# Should print "2", if I knew the name of the method.
print a.refCount()

In Python debug builds, there's the sys.getrefcount() function.

Thomas
 
A

Aahz

Is it possible to get an object to return the number of references there
are to itself?

Ex:

class test(object):
pass

a = test()
b = a

# Should print "2", if I knew the name of the method.
print a.refCount()

sys.getrefcount(a) will do the trick -- but it will print 3. Can you
figure out why?
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
 
M

Michael Hudson

Thomas Heller said:
In Python debug builds, there's the sys.getrefcount() function.

In *all* builds, there's sys.getrefcount(). Are you thinking of
sys.gettotalrefcount()?
 
T

Thomas Heller

Michael Hudson said:
In *all* builds, there's sys.getrefcount(). Are you thinking of
sys.gettotalrefcount()?

Yes, probably. Thanks for the correction.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top