yeap the point is python caches small ints (till 256) and when you define an int it returns cached small int object so "is" comparasion passes but after 256 python creates new object and "is" failed, thanks to ahmet :)
- aydın
Extra credit, explain: p=1000; q=1000; p is q
- Rob Syme
Every code object has a tuple of constants. The LOAD_CONST opcode loads a constant from this tuple onto the VM stack. So every time 1000 occurs in the program text, it refers to the same object in the tuple of constants for that block. At an interactive interpreter, each line is its own block. You can put multiple simple statements in one statement with semi-colons, so here you've forced p and q to be compiled into the same code object. If you typed three separate statements, p is q would be False. (If you put the assignment statements inside a function, they'd also share the same constants.)
- Jeremy Hylton