By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

counting references to an instance

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Brian <ba***@sympatico.ca> writes:
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
Jul 18 '05 #2

P: n/a
In article <pa*********************************@sympatico.ca> ,
Brian <ba***@sympatico.ca> wrote:

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 (aa**@pythoncraft.com) <*> 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
Jul 18 '05 #3

P: n/a
On Wed, 04 Feb 2004 14:57:59 -0500, Aahz wrote:
In article <pa*********************************@sympatico.ca> , Brian
<ba***@sympatico.ca> wrote:

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?


Does the instance of test contain a reference to itself?

Brian.
Jul 18 '05 #4

P: n/a
In article <pa*********************************@sympatico.ca> ,
Brian <ba***@sympatico.ca> wrote:
On Wed, 04 Feb 2004 14:57:59 -0500, Aahz wrote:
In article <pa*********************************@sympatico.ca> , Brian
<ba***@sympatico.ca> wrote:

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?


Does the instance of test contain a reference to itself?


Nope. Robert Brewer's response explained it.
--
Aahz (aa**@pythoncraft.com) <*> 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
Jul 18 '05 #5

P: n/a
Thomas Heller <th*****@python.net> writes:
Brian <ba***@sympatico.ca> writes:
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.


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

--
ARTHUR: Yes. It was on display in the bottom of a locked filing
cabinet stuck in a disused lavatory with a sign on the door
saying "Beware of the Leopard".
-- The Hitch-Hikers Guide to the Galaxy, Episode 1
Jul 18 '05 #6

P: n/a
Michael Hudson <mw*@python.net> writes:
Thomas Heller <th*****@python.net> writes:

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


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


Yes, probably. Thanks for the correction.

Thomas
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.