Explanation of list reference
Chris Angelico
rosuav at gmail.com
Sat Feb 15 06:31:18 EST 2014
On Sat, Feb 15, 2014 at 9:13 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> A new attempt:
Sorry, hadn't seen this when I posted.
> 0. x is x
This is the definition of identity.
> 1. if x is y then y ix x
Yes, because if x is y, there is absolutely no difference between
using one of those names or the other, in any context.
> 2. if x is y and y is z then x is z
Extension of the above. The first statement proves that you can
substitute 'x' for 'y' in the second without changing its truthiness;
therefore, based on the definition of identity, 'x is z' must be
identical to 'y is z'.
> 3. after x = y, x is y
This is the definition of assignment. (Obviously this axiom depends on
x and y being simple names and nothing tampering with the situation in
any way. But yes, this is exactly what assignment is.)
> 4. if x is y and x == x, then x == y
Yes. As in case 2, 'x is y' implies that you can substitute 'x' for
'y' or vice versa. Therefore, if x == x, then y == y, and x == y, and
y == x; because in each case, what you're doing is "object #1423443,
are you equal to object #1423443 or not?", regardless of the name you
use to access that object.
> 5. id(x) == id(y) iff x is y
This is the definition of id(). Note that it does depend on something
holding a reference to each of x and y; if it's possible for the
objects' lifetimes to not overlap, it's possible for them to reuse
ids:
>>> [1,2,3] is [2,3,4]
False
>>> id([1,2,3]) == id([2,3,4])
True
But if x and y are simple names (and therefore retaining their
referent objects), then your statement is valid.
> Does that cover it?
Largely axiomatically, yes.
ChrisA
More information about the Python-list
mailing list