Date: 2008-11-21 08:07:00
Tags: python
stupid python
Lest you be under the impression that I only bash Perl, check out the following oddity in Python: The "is" operator behaves unexpectedly with integers.
[info]xomox
2008-11-20T19:24:52Z
"[...] So it should be possible to change the value of 1."

*shudder*
[info]infinitevoid
2008-11-21T01:03:24Z
... only if integer objects are mutable. Which they aren't.
[info]ghewgill
2008-11-21T20:52:07Z
Only if you're writing C code that interfaces with Python's API, in which case it's kind of like having root on unix.
[info]paradox0220
2008-11-20T19:56:54Z
Looks like == is the solution but yeah that is really weird. Anyways, you know me, I'm happy to bash any scripting language basically.
[info]infinitevoid
2008-11-21T01:10:13Z
Eh, you get the same weird issues in Java.

String a = "Hello, World!"
String b = "Hello, World!"

String c = new StringBuilder().append("Hello, ").append("World!").toString();

assert(a == b); // These strings are interned.
assert(a != c); // c isn't.
[info]infinitevoid
2008-11-21T01:07:01Z
Yeah, "is" is used for checking object identity. And it is returning the correct values for integers... it just so happens that some integers are "interned" and some aren't. But why would you need to check the identity of your integers anyway?

[info]ghewgill
2008-11-21T20:54:03Z
I wanted to check the identity of objects, not knowing in advance whether they are numbers or not.

I was in the middle of implementing eqv? from Scheme when I ran into this.
[info]kvarko
2008-11-21T05:16:35Z
"It depends on what the meaning of 'is' is." :)

Why in heck is there an "is" operator?

[info]ghewgill
2008-11-21T20:55:02Z
Python "is" is equivalent to Java "==".
Python "==" is equivalent to Java ".equals()".

Roughly. I think.
[info]kvarko
2008-11-21T21:09:41Z
*clings to the security of his language with referential transparency*

Greg Hewgill <greg@hewgill.com>