3 ms·
CPython interns the integers between -5 and 256. See https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers https://stackove
by chrismorgan 7y ago
CPython interns the integers between -5 and 256.
See https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers https://stackoverflow.com/questions/306313/is-operator-behav... for some discussion and further related links.
- amelius 7y agoI guess it's ok if you are aware of it but this can be a source of bugs: let's say you're not aware of this "feature" and you are in the middle of writing some code and want to know whether A is A always evaluates to true for integers. So you fire up Python and type "10 is 10" which gives True. ...
- lonelappde 7y agoThat's why you should read the spec and not guess a function's definition from one example. Otherwise you'll end up thinking that almost everything is simply an alias for constant True or 0 or Error, by popping in 0 to any operator you check.
- amelius 7y agoNo. Imho, a good language designer will make it possible to learn the language incrementally without surprises. For example, a tutorial on a language should not start with "please read the spec first or you may encounter some awkward and counter-intuitive behavior in areas you thought you had already mastered".
- Alphaeus 7y agoUsing 'is' on integers raises a SyntaxWarning in 3.8+