Q about object identity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vronskij@gmail.com

    Q about object identity

    Hello,

    I am testing object identity.

    If I do it from the interpreter, I get strange results.
    >>print [] is []
    False
    >>print id([]), id([])
    3083942700 3083942700



    Why is that? Isn't this an error?


    If I test it in a script, all is OK.

    #!/usr/bin/python

    a = []
    b = []

    print a == b
    print a is b

    print id(a), id(b)

    print id(None), id(None)
    print id(True), id(True)


    On my computer, I got these results:

    True
    False
    3083769036 3083793516
    135553336 135553336
    135526444 135526444

    All as expected.


    jan bodnar


  • Christian Heimes

    #2
    Re: Q about object identity

    vronskij@gmail. com schrieb:
    Hello,
    >
    I am testing object identity.
    >
    If I do it from the interpreter, I get strange results.
    >
    >>>print [] is []
    False
    >
    >>>print id([]), id([])
    3083942700 3083942700
    >
    >
    >
    Why is that? Isn't this an error?
    No, it's not an error. You are getting this result because the list
    implementation keeps a bunch of unused list objects in a free list. It's
    an optimization trick. Python has to create two different list objects
    for "[] is []" while it can reuse the same list object for id([]) == id([]).

    Christian

    Comment

    • vronskij@gmail.com

      #3
      Re: Q about object identity

      On 3. Jún, 23:08 h., Christian Heimes <li...@cheimes. dewrote:
      vrons...@gmail. com schrieb:
      >
      Hello,
      >
      I am testing object identity.
      >
      If I do it from the interpreter, I get strange results.
      >
      >>print [] is []
      False
      >
      >>print id([]), id([])
      3083942700 3083942700
      >
      Why is that? Isn't this an error?
      >
      No, it's not an error. You are getting this result because the list
      implementation keeps a bunch of unused list objects in a free list. It's
      an optimization trick. Python has to create two different list objects
      for "[] is []" while it can reuse the same list object for id([]) == id([]).
      >
      Christian
      Aha.

      Thanks.

      jan bodnar

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Q about object identity

        On Tue, 03 Jun 2008 23:08:46 +0200, Christian Heimes wrote:
        vronskij@gmail. com schrieb:
        >Hello,
        >>
        >I am testing object identity.
        >>
        >If I do it from the interpreter, I get strange results.
        >>
        >>>>print [] is []
        >False
        >>
        >>>>print id([]), id([])
        >3083942700 3083942700
        >>
        >>
        >>
        >Why is that? Isn't this an error?
        >
        No, it's not an error. You are getting this result because the list
        implementation keeps a bunch of unused list objects in a free list. It's
        an optimization trick. Python has to create two different list objects
        for "[] is []" while it can reuse the same list object for id([]) == id([]).
        I don't think you need optimization tricks for the explanation. In ``[]
        is []`` there need to be two lists that exist at the same time to be
        compared by the ``is`` operator. With ``id([]) == id([])`` just the
        id numbers have to exist at the same time, so the execution may look like
        this:

        • create empty list
        • call `id()` with it
        • remember first id number
        • when `id()` returns, the list is not referenced anymore and can be
        garbage collected
        • create empty list, and here the list object might get allocated at the
        same memory location as the first empty list → same id.
        • call `id()` with it
        • remember second id number
        • garbage collect second empty list
        • compare both numbers

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Ethan Furman

          #5
          Re: Q about object identity

          vronskij@gmail. com wrote:
          Hello,
          >
          I am testing object identity.
          >
          If I do it from the interpreter, I get strange results.
          >
          >
          >>>>*print [] is []*
          >
          *False*
          >
          >
          >>>>print id([]), id([])
          >
          3083942700 3083942700
          >
          >
          >
          Why is that? Isn't this an error?
          >
          >
          If I test it in a script, all is OK.
          >
          #!/usr/bin/python
          >
          a = []
          b = []
          >
          print a == b
          *print a is b*
          >
          print id(a), id(b)
          >
          print id(None), id(None)
          print id(True), id(True)
          >
          >
          On my computer, I got these results:
          >
          True
          *False*
          3083769036 3083793516
          135553336 135553336
          135526444 135526444
          >
          All as expected.
          >
          >
          jan bodnar
          Assuming I'm not missing something obvious here, the results from the
          script are the same as those from the interpreter -- the _is_ returns
          False in both cases.

          Comment

          • Tommy Grav

            #6
            Re: Q about object identity


            On Jun 6, 2008, at 4:27 PM, Ethan Furman wrote:
            vronskij@gmail. com wrote:
            >Hello,
            >I am testing object identity.
            >If I do it from the interpreter, I get strange results.
            >>>>*print [] is []*
            >*False*
            >>>>print id([]), id([])
            >3083942700 3083942700
            >Why is that? Isn't this an error?
            in the first statement the two []'s are in memory at the same
            time and have different id() signatures. For the second statement
            the first id([]) is evaluated and release from memory and then
            the second id([]) is evaluated and release from memory. Since
            only one of them exist at a time they can have the same id()
            signature.

            Cheers
            Tommy

            Comment

            Working...