News for hackers. Stuff that matters.
Re:Python strings are immutable, but only sometimes by orf on Monday February 15, 2021 @04:58PM (#26148039)
`is` is for identity whereas `=` is for equality. You rarely want `is` unless you're asking if two references are the same object. This is almost exclusively used for `x is False/True`, but sometimes used to denote "missing" arguments (where true/false may be valid):

    missing = object()
    def func(a=missing):
       if a is missing:
          raise ValueError('you must pass a')
This "numbers less than 256 are the same objects" is a fairly common on the list of "wtf python" but I've never understood it. You don't use `is` like that and you would never use it in code because the operator you're using is not the right one for the thing you're trying to do.

Plus if this is the biggest wtf then that's pretty good going.

Re:Python strings are immutable, but only sometimes by cs702 on Monday February 15, 2021 @05:09PM (#26148171)
Yes, of course.

BTW, that's not the "biggest" WTF feature; it's just my favorite. There's a long list of WTF features here:

https://github.com/satwikkansal/wtfpython

Otherwise, I agree, it's a pretty good going :-)

Re:Python strings are immutable, but only sometimes by mark-r on Monday February 15, 2021 @09:48PM (#26150378)
The "numbers less than 256 are the same objects" wasn't done so you could use "is" on them, that's just a side effect. It was done as an optimization, because those small integers are far more common than the larger ones. You save space, because you need only one copy of those small integers. And you save time, because those objects are never destroyed or recreated.
Re:Python strings are immutable, but only sometimes by orf on Tuesday February 16, 2021 @01:32AM (#26151502)
Yes, of course, But I never said that it was done so you could use “is” with them, only that “is” is the wrong thing to do on them.
Re:Python strings are immutable, but only sometimes by roelschroeven on Tuesday February 16, 2021 @06:38AM (#26153005)
Of course. "is" is almost always the wrong thing to do.
Re:Python strings are immutable, but only sometimes by baud147258 on Monday February 15, 2021 @09:11PM (#26150146)
The "numbers less than 256 are the same objects" reminds me of the existence of the IntegerCache in Java, with an array storing the number from -128 to 127.
Re:Python strings are immutable, but only sometimes by pansa2 on Monday February 15, 2021 @09:42PM (#26150333)
Yes, Python has an integer cache holding the values -5 to 256.