Object Reference?

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

    Object Reference?

    I'm trying to make a graphical editor and browser for Pickled files. One
    aspect I'm not sure about is how to detect multiple references to the
    same data.

    For instance, say I had the Pickled data:
    a=[1,2,3]
    b=[a,4,5]
    c=[b,6,7]
    d=[a,b,c]

    The idea is to allow the user to browse this data and indicate
    references. In this case, if 'a' was selected, the browser should show
    that 'b', 'c', and 'd' contain a reference to 'a'. Inversely, if 'c'
    were selected, it should indicate that it's first element just isn't a
    list, but a reference to a list defined elsewhere, namely 'b'.

    Naturally, I could just recursively parse all the data comparing every
    element to every previously listed object, but is there a less obtrusive
    method? Python figures out when to delete objects based on the remaining
    references to an object. Is there a way to access this information to
    automatically lookup these references? Any help is greatly appreciated.
  • Anthony Baxter

    #2
    Re: Object Reference?

    On Fri, 06 Aug 2004 05:39:51 GMT, Chris S. <chrisks@nospam udel.edu> wrote:[color=blue]
    > Naturally, I could just recursively parse all the data comparing every
    > element to every previously listed object, but is there a less obtrusive
    > method? Python figures out when to delete objects based on the remaining
    > references to an object. Is there a way to access this information to
    > automatically lookup these references? Any help is greatly appreciated.[/color]

    Use the "id()" of the objects?

    Comment

    • Chris S.

      #3
      Re: Object Reference?

      Anthony Baxter wrote:
      [color=blue]
      > On Fri, 06 Aug 2004 05:39:51 GMT, Chris S. <chrisks@nospam udel.edu> wrote:
      >[color=green]
      >>Naturally, I could just recursively parse all the data comparing every
      >>element to every previously listed object, but is there a less obtrusive
      >>method? Python figures out when to delete objects based on the remaining
      >>references to an object. Is there a way to access this information to
      >>automatical ly lookup these references? Any help is greatly appreciated.[/color]
      >
      >
      > Use the "id()" of the objects?[/color]

      Well, I was thinking of a function that takes an object's id and returns
      the ids of all the objects that reference it. Given that Python keeps
      this information internally (I believe), my question is how do I access
      his data and what would be the easiest way to code this function?

      Comment

      • Michael Hudson

        #4
        Re: Object Reference?

        "Chris S." <chrisks@NOSPAM udel.edu> writes:
        [color=blue]
        > I'm trying to make a graphical editor and browser for Pickled
        > files. One aspect I'm not sure about is how to detect multiple
        > references to the same data.
        >
        > For instance, say I had the Pickled data:
        > a=[1,2,3]
        > b=[a,4,5]
        > c=[b,6,7]
        > d=[a,b,c]
        >
        > The idea is to allow the user to browse this data and indicate
        > references. In this case, if 'a' was selected, the browser should show
        > that 'b', 'c', and 'd' contain a reference to 'a'. Inversely, if 'c'
        > were selected, it should indicate that it's first element just isn't a
        > list, but a reference to a list defined elsewhere, namely 'b'.[/color]

        I would recommend getting initmately familiar with the format of
        pickles (something I can't claim to be). The information must be in
        there somewhere.

        Actually, maybe you could write a custom unpickler that keeps track of
        this kind of information. Something else I haven't tried :-) But,
        seriously, I think the knowledge that these objects came out of a
        particular pickle is going to be a powerful advantage for this sort of
        thing, and you should use it for everything it's worth.
        [color=blue]
        > Naturally, I could just recursively parse all the data comparing every
        > element to every previously listed object, but is there a less
        > obtrusive method? Python figures out when to delete objects based on
        > the remaining references to an object. Is there a way to access this
        > information to automatically lookup these references? Any help is
        > greatly appreciated.[/color]

        Well, there's gc.get_referrer s() and gc.get_referren ts(), but I think
        they are probably overkill (see above).

        Cheers,
        mwh

        --
        (ps: don't feed the lawyers: they just lose their fear of humans)
        -- Peter Wood, comp.lang.lisp

        Comment

        • Chris S.

          #5
          Re: Object Reference?

          Michael Hudson wrote:
          [color=blue][color=green]
          >>Naturally, I could just recursively parse all the data comparing every
          >>element to every previously listed object, but is there a less
          >>obtrusive method? Python figures out when to delete objects based on
          >>the remaining references to an object. Is there a way to access this
          >>information to automatically lookup these references? Any help is
          >>greatly appreciated.[/color]
          >
          >
          > Well, there's gc.get_referrer s() and gc.get_referren ts(), but I think
          > they are probably overkill (see above).[/color]

          Thanks, these are exactly what I need. Pickle is just the file format.
          The data are Python constructs, so I shouldn't have to bother with the
          format at all, since I'm simply converting the Pickled file into generic
          Python data. Plus, writing my own Pickle parser would be hugely
          complicated since Pickle is essentially its own small programming language.

          Comment

          • Gandalf

            #6
            Re: Object Reference?

            > I'm trying to make a graphical editor and browser for Pickled files.[color=blue]
            > One aspect I'm not sure about is how to detect multiple references to
            > the same data.[/color]

            ....
            [color=blue]
            > For instance, say I had the Pickled data:
            > a=[1,2,3]
            > b=[a,4,5]
            > c=[b,6,7]
            > d=[a,b,c]
            >
            > The idea is to allow the user to browse this data and indicate
            > references. In this case, if 'a' was selected, the browser should show
            > that 'b', 'c', and 'd' contain a reference to 'a'. Inversely, if 'c'
            > were selected, it should indicate that it's first element just isn't a
            > list, but a reference to a list defined elsewhere, namely 'b'.[/color]

            [color=blue][color=green][color=darkred]
            >>> Naturally, I could just recursively parse all the data comparing every
            >>> element to every previously listed object, but is there a less
            >>> obtrusive
            >>> method? Python figures out when to delete objects based on the
            >>> remaining
            >>> references to an object. Is there a way to access this information to
            >>> automatically lookup these references? Any help is greatly appreciated.[/color]
            >>
            >>
            >> Use the "id()" of the objects?[/color]
            >
            >
            > Well, I was thinking of a function that takes an object's id and
            > returns the ids of all the objects that reference it. Given that
            > Python keeps this information internally (I believe), my question is
            > how do I access his data and what would be the easiest way to code
            > this function?[/color]

            Yes, Python figures out when to delete an object, based on its reference
            count. But Python only counts the references. It keeps track of the
            number of references for the object. It does not know where the
            reference is located in memory. The short answer is that you cannot get
            this information effectively.

            Long answer follows.

            Question: There can be many objects in the structure without a name
            (e.g. not referenced from the value part of a dictionary).
            How do you want to display them? As an example:

            class A:
            pass

            a = A() # THE object
            b = [a,a,a,a]
            del a

            # At this point, THE object does not have a named reference at all but
            it is referenced four times. Do you really want to identify the objects
            with their memory address? The id function does this to you (Anthony
            Baxter said the same.) But this will be a useless pickle browser since
            nobody will notice memory address duplications.

            Another big problem here. In Python, the only things you can have are an
            object references. You cannot name the object itself. There are no
            'object variables', only 'reference variables'. For example:

            class A:
            pass

            a = A() # THE object we are talking about
            b = [a]
            c = [b]

            Here is the question: does the object b "references " a? The answer is
            that the question is wrong because 'a' is a reference itself - a
            reference can only reference one object at a time. Good questions are:

            - does 'a' and 'b' reference to the same object? (No)
            - does 'b[0]' and 'a' reference to the same object? (Yes)

            You may think that a reference of 'a' is contained in 'b'. But this is
            not a general assumption. For example:

            a = A()
            b = A()
            a.o = b
            b.o = a

            Now which is contained in which?

            Sorry for the long e-mail.

            G



            Comment

            • Chris S.

              #7
              Re: Object Reference?

              Gandalf wrote:
              [color=blue]
              > Yes, Python figures out when to delete an object, based on its reference
              > count. But Python only counts the references. It keeps track of the
              > number of references for the object. It does not know where the
              > reference is located in memory. The short answer is that you cannot get
              > this information effectively.[/color]

              The gc module has methods that list an object's referrers and referents.
              Although you're correct in that we're unable to access the total number
              of references since duplicate references are not counted. For instance

              import gc
              a=[1,2,3]
              b=[a,a,a,a]
              print gc.get_referrer s(a)

              would only print out one instance of b, even though it references 'a'
              four times. However, this only seems to apply for direct references, so
              the total number of references can be determined by scanning all
              referrers and counting the duplicate ids.

              Comment

              Working...