sameness/identity

John Roth johnroth at ameritech.net
Sun Sep 30 22:26:25 EDT 2001


"Greg Weeks" <weeks at vitus.scs.agilent.com> wrote in message
news:1001876090.367103 at cswreg.cos.agilent.com...
> 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)).  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.
>
> Now consider a simple Bank_account class:
>
>     class Bank_account:
> def __init__(me, initial_balance):
>     me.balance = initial_balance
> def deposit(me, amount):
>     me.balance += amount
> def withdraw(me, amount):
>     me.balance -= amount
> def get_balance(me):
>     return  me.balance
>
>     my_bank_account = Bank_account(100)
>     your_bank_account = Bank_account(100)
>
> Are the two bank accounts the same bank account?  No.
<rest of discussion snipped>

I think you're confusing yourself here. If I was doing a bank account
program, I'd expect that there was at most one instance of the
bank account class active for a given bank, department and
account. More than that and you risk all sorts of update anomalies.

In this case, 'is' is what you need. You can make == work like
is, and I probably would just to insure that I didn't accidentally
introduce errors (and also because I need to provide ordering
operators for report sorting, etc.)

I suspect that one of the design criteria for a well-formed design
is that there be some kind of identification method to tell whether
instances refer to the same conceptual object. The '=='
operator on a class should implement that method. 'is' only works
if the 'at most one instance per conceptual object' rule is followed.

John Roth

>
> Greg





More information about the Python-list mailing list