The Descriptor Protocol and Python Black Magic
faingezicht.com
faingezicht.com
>>> a = 100
>>> b = 100
>>> a is b
True
>>> a = 1000
>>> b = 1000
>>> a is b
FalseThe case you point out is because the python bytecode compiler will intern raw values for some items - e.g. string literals, some integer values, and so on. More on interning: https://en.wikipedia.org/wiki/String_interning
The reason, in your example, that `is` returns different results is that small integers are "interned"; i.e., the literal 100 always references the exact same integer object, whereas 1000 will cause an integer to be allocated. (So a and b, while both represented 1000, are stored twice in memory.)
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def b(self): pass
...
>>> A.b
<unbound method A.b>
>>> hex(id(A.b))
'0x104e2c0a0'
>>> hex(id(A.b))
'0x104daeb90'
>>> hex(id(A.b))
'0x104daeb90'
So the `x is y` approach is "safer." :) >>> some_unbound_method = A.b
>>> hex(id(some_unbound_method))
'0x104e2c0a0' >>> foo = A.b
>>> bar = A.b
>>> hex(id(foo))
'0xdeadbeef'
>>> hex(id(bar))
'0xdecafcab'
>>> foo is bar
FalseIs there a place for raising documentation bugs?