A question on modification of a list via a function invocation

Steve D'Aprano steve+python at pearwood.info
Thu Sep 7 22:09:25 EDT 2017


On Fri, 8 Sep 2017 04:24 am, Rustom Mody wrote:

> On Thursday, September 7, 2017 at 6:52:04 PM UTC+5:30, Gregory Ewing wrote:
>> Rustom Mody wrote:
>> 
>> > I said: In that case please restate the definition of 'is' from the manual
>> > which invokes the notion of 'memory' without bringing in memory.
>> 
>> I don't know whether it's in the manual, but at least for
>> mutable objects, there is a way to define the notion of
>> "same object" that doesn't require talking about "memory":
>> 
>> Two names refer to the same object if and only if mutations
>> made through one are visible through the other.
> 
> Seems a sensible comment!


Except that it is wrong, or at least over-generalised. It is trivially easy to
show false positives:

py> class K: # defines an object
...     def __init__(self, x):
...             self.x = x
...     def append(self, value):
...             self.x.append(value)
...
py> a = []
py> b = K(a)
py> a is b  # these are not the same object (they're different types)
False
py> b.append(99)  # but modifying b modifies a
py> a
[99]


Rustom, I've already given you the definitive answer to your question about how
to define `is` without talking about memory. You haven't replied or
acknowledged it, so here is it again:

`is` compares the two operands for identity. If the two operands are the same
object, `is` returns True, if they are distinct objects, `is` returns False.


This does require that we agree on "same object", which as you point out is (in
its full generality) a difficult thing to define. E.g. in the past I've raised
the paradox of My Grandfather's Axe.

But the intuitive, common-sense notion of "same object" is, I think, sufficient
here. If you want to argue that it is *not* sufficient, I think it's up to you
to demonstrate a problem with the definition.

Can you show an actual false positive (two distinct objects for which `is`
returns True) or false negative (the same object given as both operands for
`is` nevertheless returns False)? In the absence of any actual bugs in the
definition, I maintain that it is sufficient.

And if there are still philosophical problems with the concept of "the same
object", they either don't apply to Python objects, or they apply equally to
all objects in the universe and there's nothing we can do about it. Either way,
the problem of defining the Python `is` operator without referring to memory is
solved.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list