Why this difference?

Discussion in 'Python' started by n00m, Feb 24, 2011.

  1. n00m

    n00m Guest

    file my.txt:
    ===============================
    0 beb
    1 qwe
    2 asd
    3 hyu
    4 zed
    5 asd
    6 oth
    =============================


    py script:
    ===============================
    import sys

    sys.stdin = open('88.txt', 'r')
    t = sys.stdin.readlines()
    t = map(lambda rec: rec.split(), t)
    print t
    print t[2][1] == t[5][1]
    print t[2][1] is t[5][1]
    print '=================================='
    a = 'asd'
    b = 'asd'
    print a is b


    output:
    =======================================
    [['0', 'beb'], ['1', 'qwe'], ['2', 'asd'], ['3', 'hyu'], ['4', 'zed'],
    ['5', 'as
    d'], ['6', 'oth']]
    True
    False
    ==================================
    True
     
    n00m, Feb 24, 2011
    #1
    1. Advertising

  2. n00m

    n00m Guest

    The 1st "False" is not surprising for me.
    It's the 2nd "True" is a bit hmmm... ok, it doesn't matter
    ======================
    Have a nice day!
     
    n00m, Feb 24, 2011
    #2
    1. Advertising

  3. Den 24.02.11 13.41, skrev n00m:
    > The 1st "False" is not surprising for me.
    > It's the 2nd "True" is a bit hmmm... ok, it doesn't matter
    > ======================
    > Have a nice day!


    I am no expert, but I think python re-uses some integer and string
    objects. For instance, if you create the object int(2) it may be re-used
    later if you have several 2 objects in your code. This is to save some
    memory, or some other performance hack. Don't rely on it.

    For instance:
    >>> a = 100
    >>> b = 100
    >>> a is b

    True
    >>> a = 2**60
    >>> b = 2**60
    >>> a is b

    False

    Strange, but it's just like this!

    Paul
     
    Paul Anton Letnes, Feb 24, 2011
    #3
  4. n00m

    n00m Guest

    > Don't rely on it.

    Hmm.... I never was about to rely on it.
    Simply sorta my academic curiosity.
     
    n00m, Feb 24, 2011
    #4
  5. n00m

    Terry Reedy Guest

    On 2/24/2011 7:19 AM, n00m wrote:
    > file my.txt:
    > ===============================
    > 0 beb
    > 1 qwe
    > 2 asd
    > 3 hyu
    > 4 zed
    > 5 asd
    > 6 oth
    > =============================
    >
    >
    > py script:
    > ===============================
    > import sys
    >
    > sys.stdin = open('88.txt', 'r')
    > t = sys.stdin.readlines()
    > t = map(lambda rec: rec.split(), t)
    > print t
    > print t[2][1] == t[5][1]
    > print t[2][1] is t[5][1]
    > print '=================================='
    > a = 'asd'
    > b = 'asd'
    > print a is b
    >
    >
    > output:
    > =======================================
    > [['0', 'beb'], ['1', 'qwe'], ['2', 'asd'], ['3', 'hyu'], ['4', 'zed'],
    > ['5', 'as
    > d'], ['6', 'oth']]
    > True
    > False
    > ==================================
    > True


    An implementation may *optionally* cache immutable values -- and change
    its internal rules as it pleases. When creating string objects from
    literals that look like identifiers, CPython does this, but apparently
    not when splitting an existing string.

    --
    Terry Jan Reedy
     
    Terry Reedy, Feb 24, 2011
    #5
  6. n00m

    n00m Guest

    @nn, @Terry Reedy:

    Good reading. Thanks. In fact now the case is closed.
     
    n00m, Feb 24, 2011
    #6
  7. On Thu, 24 Feb 2011 13:58:28 +0100, Paul Anton Letnes wrote:

    > Den 24.02.11 13.41, skrev n00m:
    >> The 1st "False" is not surprising for me. It's the 2nd "True" is a bit
    >> hmmm... ok, it doesn't matter ======================
    >> Have a nice day!

    >
    > I am no expert, but I think python re-uses some integer and string
    > objects. For instance, if you create the object int(2) it may be re-used
    > later if you have several 2 objects in your code. This is to save some
    > memory, or some other performance hack. Don't rely on it.


    Absolutely correct.

    It can be quite surprising when Python re-uses objects. E.g this
    surprised me:

    >>> x, y = "hello world", "hello world"
    >>> x == y, x is y

    (True, True)

    compared to this:

    >>> x = "hello world"
    >>> y = "hello world"
    >>> x == y, x is y

    (True, False)


    Don't rely on *any* of this, it's subject to change without notice.



    --
    Steven
     
    Steven D'Aprano, Feb 25, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,390
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    921
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  4. Nick Timkovich
    Replies:
    0
    Views:
    73
    Nick Timkovich
    Feb 25, 2014
  5. Skip Montanaro
    Replies:
    0
    Views:
    71
    Skip Montanaro
    Feb 25, 2014
Loading...

Share This Page