News for hackers. Stuff that matters.
Re:First Impressions of Python from a Java Developer by torme on Monday November 08, 2010 @02:48PM (#1883713)
Totally nitpicking here but != and == should typically only be used when comparing primitive values.

For example, if you have an Integer object (not a primitive int) you should use .equals() and not == for comparison. The == is used for comparing references and not values.

That said, this only illustrates some extra complexity in Java.

Re:First Impressions of Python from a Java Developer by Jabbles on Monday November 08, 2010 @03:00PM (#1883751)
Argh, the complexities of Java!

Integer a = new Integer(10);

Integer b = new Integer(10);

Integer c = new Integer(1000);

Integer d = new Integer(1000);

a==b is true

c==d is false

Re:First Impressions of Python from a Java Developer by reinhardt on Monday November 08, 2010 @07:24PM (#1884709)
Sorry to break it out to you but you picked a bad example; the equivalent Python code has the same WTF effect:

Python 2.6.6 (r266:84292, Oct 17 2010, 15:53:05)

[GCC 4.4.3] on linux2

>>> a = int(10)

>>> b = int(10)

>>> c = int(1000)

>>> d = int(1000)

>>> a is b

True

>>> c is d

False

Re:First Impressions of Python from a Java Developer by Jabbles on Tuesday November 09, 2010 @05:11AM (#1886002)
Wow! Really? (I'm not a Pythonator.)

Same reasoning as Java?

Re:First Impressions of Python from a Java Developer by reinhardt on Tuesday November 09, 2010 @12:49PM (#1887243)
Basically yes, although it is not part of the language definition and should not be relied on. It's just an implementation artifact of CPython, and not only that but the threshold of which integers are cached changes for different Python versions.
Re:First Impressions of Python from a Java Developer by jrockway on Monday November 08, 2010 @03:45PM (#1883921)
It's simple, right? The result of <Integer> == <Integer> is undefined with respect to the value that is stored in the Integer object.
Re:First Impressions of Python from a Java Developer by RodgerTheGreat on Monday November 08, 2010 @06:07PM (#1884443)
A bit more subtle than that- Java reserves singleton instances of Integer for values in the range -128 to 127. In these cases, the Integers will contain the same value and refer to the same object, so == works. It's not actually undefined behavior.
Re:First Impressions of Python from a Java Developer by jonpaul on Monday November 08, 2010 @02:58PM (#1883744)
Maybe. C# uses != and == for string comparisons. Maybe the question should be: should an immutable string class be considered primitive considering how frequently it's used despite the fact it's internally passed by ref? I guess that debate is fairly academic and not very useful. Regardless, I agree with the author's point. I don't like having to use the "equals" method in Java for strings. It feels a bit outdated.
Re:First Impressions of Python from a Java Developer by torme on Monday November 08, 2010 @03:07PM (#1883773)
Agreed.

Thinking about it now, I would rather the inverse in a language be true, and have == be used for value comparison, and have a more wordy function for reference comparison. In my experience, I feel that its a much more common usage, and would probably reduce errors caused by programmers choosing the wrong operator. The only downside to this is that you then have to override an operator if you want to change equality comparison, instead of a function.

Re:First Impressions of Python from a Java Developer by Xuzz on Monday November 08, 2010 @07:09PM (#1884654)
Python has == for value, and 'is' for reference -- that seems like a good solution to me, since it works for both "primitive" and "complex" data structures.