Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why this difference?

Reply
Thread Tools

Why this difference?

 
 
n00m
Guest
Posts: n/a
 
      02-24-2011
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



 
Reply With Quote
 
 
 
 
n00m
Guest
Posts: n/a
 
      02-24-2011
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!
 
Reply With Quote
 
 
 
 
Paul Anton Letnes
Guest
Posts: n/a
 
      02-24-2011
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
 
Reply With Quote
 
n00m
Guest
Posts: n/a
 
      02-24-2011
> Don't rely on it.

Hmm.... I never was about to rely on it.
Simply sorta my academic curiosity.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-24-2011
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

 
Reply With Quote
 
n00m
Guest
Posts: n/a
 
      02-24-2011
@nn, @Terry Reedy:

Good reading. Thanks. In fact now the case is closed.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments