sameness/identity

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Sep 30 18:27:48 EDT 2001


weeks at vitus.scs.agilent.com (Greg Weeks) writes:

> I said in an earlier note that I didn't want to get into the whole issue of
> "sameness" and how Python "got it wrong".  Well, I changed my mind.  I'll
> first admit that "got it wrong" is a poor choice of words.  Allow me to
> explain what I meant.
> 
> Consider:
> 
>     x = 6.02
>     y = 6.02
> 
> Are x and y identical?  They are identical as *numbers* (which I'll call
> conceptual identity) but not as Python *implentations* of numbers (since
> id(x) != id(y)).  

Well, it depends on your notion of mathematics. Most mathematicians
would readily acknowledge that x is equal to y. When asked whether
they are identical, they'd ask 'in what calculus'?

In the initial algebra model, 'values' are terms which may be equal.
For example, +(1,2) is equal to -(4,1), which is equal to 3. Terms are
identical if they have the same structure. In that sense, x and y
would be identical. However, Python does not use the initial algebra
model.

Instead, Python uses an OO model for numbers. Each number is an
object, i.e. a combination of state and behaviour. Identity is
well-defined in OO; and object is identical only to itself. Equality
is difficult in general. For numbers it is somewhat easier. If two
numbers have the same type, they are equal iff they have the same
state.

> Good programming style requires us to think of numbers primarily as
> numbers and not as their implementations.  So we are more interested
> in conceptual identity than in implementation identity.

For numbers, we are primarily concerned with equality, not with
identity.

> Are the two bank accounts the same bank account?  No.  (How would you feel,
> for example, if I withdrew money from your bank account?)  x and y are
> conceptually the same, but my_bank_account and your_bank_account are not.

No, they are not conceptually the same. They are equal. The bank
accounts are certainly not identical (being different objects); they
are also not equal (bank accounts are equal iff they belong to the
same bank and have the same account number).

> Why is (conceptual) identity of numbers different from (conceptual)
> identity of bank accounts?  

They have the same notion of identity. An object is identical only to
itself. Thus, the bank accounts, and x and y are all pairwise not
identical.

> Because bank accounts are mutable.  A bank account can have
> different states at different times.  A number can't.

That is a misleading analysis. Do you think tuples are "conceptually
identical" if the have equal content? Mutability has nothing to do
with identity.

> So, we've encountered three kinds of identity in this discussion:
> 
>     A.  conceptual identity
>     B.  implementation identity
>     C.  state identity

You mentioned A, but I don't share this concept with you. What you
call C is a special kind of equality, not identity.

Regards,
Martin



More information about the Python-list mailing list