Why this difference?

N

n00m

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
 
N

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

Paul Anton Letnes

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

Strange, but it's just like this!

Paul
 
N

n00m

Don't rely on it.

Hmm.... I never was about to rely on it.
Simply sorta my academic curiosity.
 
T

Terry Reedy

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

Steven D'Aprano

Den 24.02.11 13.41, skrev n00m:

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:
(True, True)

compared to this:
(True, False)


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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top