by kbp · 7y ago · 0 comments
You still need to have a grasp on the difference between reference equality and value equality without getting into anything anyone would call tricks or implementation details (eg, after `x = []; y = []; x.append(1)`, how many elements does y have?).
Python should have prevented checking primitives with is though -- only let object pointers...
That would have been less surprising (since for < 256 the singleton-ness means all same valued instances have the same "address" (id), so behave in equality check as if they were plain numbers).
[0]: https://github.com/python/cpython/blob/master/Objects/longob...
too many similar demos
by the way I actually like python and use it often, just saying it can surprise you when you start, not intuitive per se until you become good at it.
Perhaps use x = x + [1] instead?
But yeah, surprises like this (unexpected return values) are actually a pretty strong case for using a language like Rust or C++ where the compiler will tell you this before you run the program.
x=[1,2],id(x), x+=[3], id(x) ; # same id, means x stay at the original location
there should be a blog page to list those surprises for beginners :)
It’s a disaster for newcomers to programming in general. I am pretty sure a seasoned developer in any other language would understand why that returns None, too.