473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Equality and identity

In python, it seems like immutable objects are equal under both
equality and identity:
5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True

But that isn't true for tuples: (5,2) == (5,2) True (5,2) is (5,2)

False

Why not? Are tuples mutable in some way I've missed, or are there
other reasons for this behaviour? To me, it seems natural to concider
(5,2) and (5,2) the same object, just like "5,2" and "5,2".
Marius
--
"Who needs crypto WYCWLT?"
-- William Newman
Jul 18 '05 #1
6 1961
In article <3c*************@nelja.ifi.uio.no>,
Marius Bernklev <ma**************@ifi.uio.no> wrote:
In python, it seems like immutable objects are equal under both
equality and identity:
5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True

But that isn't true for tuples: (5,2) == (5,2) True (5,2) is (5,2)

False

Why not? Are tuples mutable in some way I've missed, or are there
other reasons for this behaviour? To me, it seems natural to concider
(5,2) and (5,2) the same object, just like "5,2" and "5,2".


Note that the Python interpreter is a program, with finite
limits on its implementation. It is not an algebraic system.
If you find that (5,2) and (5,2) are not the same object,
rather than trying to infer properties of types from this,
first you might ask `what would it take, as implementor of
Python, to always use the same object for these two quantities?'
and, `how important is it to do so?' This may lead you to
make a few more experiments with strings and find some holes
in your identity proposition there, too.

Don't make the mistake that people seem recently so prone to
make, of an unhealthy preoccupation with "is" and identity.
When you need to test for equality, always use "==".

Donn Cave, do**@u.washington.edu
Jul 18 '05 #2
At some point, Marius Bernklev <ma**************@ifi.uio.no> wrote:
In python, it seems like immutable objects are equal under both
equality and identity:
5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True

But that isn't true for tuples: (5,2) == (5,2) True (5,2) is (5,2) False

Why not? Are tuples mutable in some way I've missed, or are there
other reasons for this behaviour? To me, it seems natural to concider
(5,2) and (5,2) the same object, just like "5,2" and "5,2".


We've had this question recently. Immutable objects *may* be reused,
but are not required. It's implementation-dependent.

Rememeber:
'is' checks object identity
'==' checks object equality

identity implies equality, but not the other way around.

In your example, 5 and "hei", for various reasons, are compiled to
reference the exact same object, so 'is' returns True. However, no
such caching is done with tuples, so (5,2) and (5,2) compile to
references to different objects (whose contents are equal).

Here are some examples that may help some more:
a = "hei x"
b = "hei x"
a is b False
# The space in "hei x" prevents the string from being interned, so two
# string objects are created) a = 12345
b = 12345
a is b False
# integers outside of (about) [-100,100] are not cached, so a and b
# reference different objects) 12345 is 12345 True
# huh? let's look closer... import dis
c = compile('12345 is 12345', '<string>', 'single) # this creates the code object that is executed to evaluate '12345 is 12345' exec c True dis.dis(c) 1 0 LOAD_CONST 0 (12345)
3 LOAD_CONST 0 (12345)
6 COMPARE_OP 8 (is)
9 PRINT_EXPR
10 LOAD_CONST 1 (None)
13 RETURN_VALUE
# ahh, the compiler optimizes storing constants in the code object. c = compile('(5,2) is (5,2)', '<string>', 'single')
dis.dis(c) 1 0 LOAD_CONST 0 (5)
3 LOAD_CONST 1 (2)
6 BUILD_TUPLE 2
9 LOAD_CONST 0 (5)
12 LOAD_CONST 1 (2)
15 BUILD_TUPLE 2
18 COMPARE_OP 8 (is)
21 PRINT_EXPR
22 LOAD_CONST 2 (None)
25 RETURN_VALUE
# here we see the compiler optimizes storage for the numbers also, but
# since it builds tuples, instead of storing them in the code object,
# it doesn't store (5,2) as a one object, but constructs two different
# ones that it compares.
Also, here's a example with Jython 2.1:
Jython 2.1 on java1.3 (JIT: kaffe.jit)
Type "copyright", "credits" or "license" for more information. 12345 is 12345 1 "hei" is "hei" 1 a = "hei"
b = "hei"
a is b

0

Note that the last part gives True when done with CPython, as "hei"
looks like it could be an identifier, and is 'interned' (cached),
whereas Jython doesn't do that.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #3
co**********@physics.mcmaster.ca (David M. Cooke) writes:
In your example, 5 and "hei", for various reasons, are compiled to
reference the exact same object, so 'is' returns True. However, no
such caching is done with tuples, so (5,2) and (5,2) compile to
references to different objects (whose contents are equal).


Thanks!
Marius
--
"Who needs crypto WYCWLT?"
-- William Newman
Jul 18 '05 #4
On Tue, 30 Mar 2004 22:12:41 +0200, Marius Bernklev wrote:
co**********@physics.mcmaster.ca (David M. Cooke) writes:
In your example, 5 and "hei", for various reasons, are compiled to
reference the exact same object, so 'is' returns True. However, no
such caching is done with tuples, so (5,2) and (5,2) compile to
references to different objects (whose contents are equal).


Thanks!


Also note that this is implementation-dependent, and is not guaranteed
to remain the same for any given type. If you code such that you are
depending on any particular identity of equal values, your code becomes
brittle and will break when a different optimisation scheme is used in
the Python implementation.

--
\ "It's a small world, but I wouldn't want to have to paint it." |
`\ -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #5
Marius Bernklev <ma**************@ifi.uio.no> wrote in message news:<3c*************@nelja.ifi.uio.no>...
In python, it seems like immutable objects are equal under both
equality and identity:
5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True

x = 100
x is 100

False
Jul 18 '05 #6
Marius Bernklev <ma**************@ifi.uio.no> wrote in message news:<3c*************@nelja.ifi.uio.no>...
In python, it seems like immutable objects are equal under both
equality and identity:
5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True


It only seems that way. Note:
a = 5
b = 5
100 is 100 True #but:
a = 100
b = 100
a is b False #but also!:
a = 100;b = 100
a is b True # for strings:
a = 'hello'
b = 'hello'
a is b True a = 'hello world'
b = 'hello world'
a is b False a = 'hello world';b = 'hello world'
a is b

True

As was pointed out, this is implementation dependant. Our favourite
implementation interns small integers and simple strings (and
apparently also equivalent strings and integers created on the same
line(?)), for optimisation purposes. These are really exceptional.
You shouldn't find this behaviour in defined classes for instance.
The behaviour of tuples in this regard as much more 'normal.'
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
6
by: benben | last post by:
I am a C++ guy recently migrated to C#. One of the thing I don't understand is assignment (=) and equality (==) operators. If I try to do the following: C a = new C; C b = a; Then I will get...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
3
by: toton | last post by:
Hi, I have a struct Point { int x, int y; } The points are stored in a std::vector<Pointpoints; (global vector) I want to add equality (operator == ) for the point, which will check equality...
0
by: Christian Heimes | last post by:
Good Z schrieb: Never use the identity operator "is" on strings and numbers! In most of the cases it won't do what you are expecting. The behavior can be explained with with a simple analogy....
5
by: jehugaleahsa | last post by:
Hello: Is there a way to force a Dictionary to determine equivilence by memory location? I have a generic class where I don't want to treat equal items as identical items. Only when two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.