counting references to an instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    counting references to an instance

    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.
  • Thomas Heller

    #2
    Re: counting references to an instance

    Brian <balex@sympatic o.ca> writes:
    [color=blue]
    > 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()
    >[/color]

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

    Thomas

    Comment

    • Aahz

      #3
      Re: counting references to an instance

      In article <pan.2004.02.04 .18.55.02.38509 2.6672@sympatic o.ca>,
      Brian <balex@sympatic o.ca> wrote:[color=blue]
      >
      >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()[/color]

      sys.getrefcount (a) will do the trick -- but it will print 3. Can you
      figure out why?
      --
      Aahz (aahz@pythoncra ft.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

      Comment

      • Brian

        #4
        Re: counting references to an instance

        On Wed, 04 Feb 2004 14:57:59 -0500, Aahz wrote:
        [color=blue]
        > In article <pan.2004.02.04 .18.55.02.38509 2.6672@sympatic o.ca>, Brian
        > <balex@sympatic o.ca> wrote:[color=green]
        >>
        >>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()[/color]
        >
        > sys.getrefcount (a) will do the trick -- but it will print 3. Can you
        > figure out why?[/color]

        Does the instance of test contain a reference to itself?

        Brian.

        Comment

        • Aahz

          #5
          Re: counting references to an instance

          In article <pan.2004.02.04 .21.00.59.32256 4.7241@sympatic o.ca>,
          Brian <balex@sympatic o.ca> wrote:[color=blue]
          >On Wed, 04 Feb 2004 14:57:59 -0500, Aahz wrote:[color=green]
          >> In article <pan.2004.02.04 .18.55.02.38509 2.6672@sympatic o.ca>, Brian
          >> <balex@sympatic o.ca> wrote:[color=darkred]
          >>>
          >>>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()[/color]
          >>
          >> sys.getrefcount (a) will do the trick -- but it will print 3. Can you
          >> figure out why?[/color]
          >
          >Does the instance of test contain a reference to itself?[/color]

          Nope. Robert Brewer's response explained it.
          --
          Aahz (aahz@pythoncra ft.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

          Comment

          • Michael Hudson

            #6
            Re: counting references to an instance

            Thomas Heller <theller@python .net> writes:
            [color=blue]
            > Brian <balex@sympatic o.ca> writes:
            >[color=green]
            > > 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()
            > >[/color]
            >
            > In Python debug builds, there's the sys.getrefcount () function.[/color]

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

            --
            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

            Comment

            • Thomas Heller

              #7
              Re: counting references to an instance

              Michael Hudson <mwh@python.net > writes:
              [color=blue]
              > Thomas Heller <theller@python .net> writes:[color=green]
              >>
              >> In Python debug builds, there's the sys.getrefcount () function.[/color]
              >
              > In *all* builds, there's sys.getrefcount (). Are you thinking of
              > sys.gettotalref count()?[/color]

              Yes, probably. Thanks for the correction.

              Thomas

              Comment

              Working...