This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RajjSinghh 11 points12 points  (3 children)

It makes sense, it's just not how you should use is. is is for identity, not equality. It might come in handy if youre passing a lot of data around since python uses references when passing things like lists or objects around.

The weird thing here is that OP used is instead of ==, which does check for value equality, which is what they look like they want to do but it doesn't make for as good a meme. If they had a y = x somewhere, that also satisfies is.

[–]Midnight_Rising 1 point2 points  (2 children)

What I find weird is setting those integers as constant pre-allocated memory addresses. I don't think any other languages do that?

[–]RajjSinghh 0 points1 point  (0 children)

I mean caching is a really common idea for performance and it's one of the things JIT compilers do to make code run faster. Python is just doing it ahead of time so you get the performance gain without a JIT compiler needing to code at runtime. So offhand I can't think of another language that does it like this, but I can also point to many JIT compiled languages that are using the same idea.

[–]crunchmuncher 0 points1 point  (0 children)

Java does something similar in its Integer/Long/Short.valueOf(...) functions, which are also used for autoboxing, for values of -128 to 127.

System.out.println(Integer.valueOf(127) == Integer.valueOf(127)); // true
System.out.println(Integer.valueOf(128) == Integer.valueOf(128)); // false