4 ms·
a==b would be true. The is operator is not the same as the equality operator The is operator checks to see if both point to the same area in memory. Small numb
by el_oni 5y ago
a==b would be true. The is operator is not the same as the equality operator
The is operator checks to see if both point to the same area in memory. Small numbers are preallocated in python so they point to the same memory.
>>>a=300
>>>b=a
>>>a is b
True
- amitport 5y agoOf course there is some internal logic. The point was that both languages have pitfalls.
- h8hawk 5y agoIn python it completely makes sense because it's an object oriented language and threat everything as object. In fact it's very consistent about that. Even classes are object and instance of metaclass. Difference between '==' and 'is' is came from that. This is not the case with Javascript. It's sad many peoples who are dealing with python don't understand that.
- amitport 5y ago"-In python- it completely makes" It is a pitfall. It's not about what python developers think about it. It is about what people from other languages experience when they learn the language. (Though it's not my main point, I want to add as (also) a python developer, that I think this is a terrible design choice, which could easily be avoided. In this case python doesn't "threat everything as object" it treats small int literals differently than bigger int literals)
- h8hawk 5y agoSmall integeres are object in python. They are cached in some implemention. If you only care about shallow syntax, yes it's confusing. But if you care more about semantics, it's completely consistent and doesn't make especial case.
- deleted 5y ago[deleted]
- amitport 5y agoMany programmers do care about the "shallow" syntax. Regardless, if, as you said, the operational semantics of the language is inconsistent among implementations and depends on internal caching, it only makes it worse.
- egeozcan 5y agojsc >> 16 === [16] false