We've all been complaining about javascript, but python might be worse

Posted Jul 7, 2018, 1:46 AM UTC
3312

Posted Jul 7, 2018, 1:46 AM UTC
Image Transcription: Code
a = 42
b = 42
a == b # => True
a is b # => True
c = 4242
d = 4242
c == d # => True
c is d # => False
^^I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
All I see is JavaScript and php hate. Cant really figure out why...
[deleted]
You know you're on programmer humor, right?
[deleted]
I just feel that you're taking it a bit seriously. Everyone here complains or makes fun of their favourite languages
[deleted]
I'm talking about the dozens of posts every hour in this sub complaining how {} + [] == 0 or something.
[deleted]
[deleted]
[deleted]
See https://stackoverflow.com/questions/13650293/understanding-pythons-is-operator Makes total sense to me
Let the language handle it how it thinks it is the best. Use the is operator only if you want to check for reference equality and not for content equality
Still doesn't make sense in this picture. A isn't shown to be assigned to B but 'is' still prints True. Why does repeated assignment to 42 point to the same '42' object but 4242 produces two different objects?
Wat
Because larger numbers are not cached in an internal map by the language, but small numbers are.
Edit: if the language did so, the application would end up using much more memory than it needs. Consider you have a loop from 1 to 1,000,000 do you want to keep all the numbers on the heap, long after you finished using them? That would be a memory leak by definiton
[deleted]
This ^ lmao
Java caches smaller Integers (only boxed?) as well.
That makes sense, but it doesn't make sense that 4242 is a large number. It makes sense to limit small numbers close to the size of a single word of memory which is typically 64-bits nowadays. Even in 32-bit architectures, 4242 is well within the limits.
Its not really about how large the number is but how often it is used. Numbers in the range 0-100 are more likely used than numbers greater than 100 (I dont know if 100 is the threshold in Python but you get the idea)