[Python-Dev] redefining is

Andrew Koenig ark at acm.org
Tue Mar 23 22:47:24 EST 2004


> [Shane Hathaway]
> As I understood it, 'b' and 'c' are supposed to be substitutable in
> the following example, since 'b' and 'c' will have the same value
> even after 'a' changes:
>
> a = []
> b = (a,)
> c = (a,)

> [Tim Peters]
> Fair enough.  Do interesting/valuable use cases appear then?

Depends on what you consider interesting or valuable :-)

If I understand things correctly, if I evaluate

	x = (3, 4)
	y = (3, 4)

the implementation is permitted to bind x and y to the same object, or not,
as it pleases.  If you like, all instances of (3, 4) are equivalent and the
implementation is permitted to chose any representative of that equivalence
class.

I think the interesting use case is whenever one wants to test for
membership in that equivalence class.

Now, you will probably say that == does the trick: if x is a member of the
equivalence class of (3, 4), then x == y is True if and only if y is also a
member of that equivalence class.

However, consider this:

	a = []
	x = (3, a)
	y = (3, a)

I claim that x and y are *still* members of an equivalence class in exactly
the same sense that they were members before -- aside from checking the
identity of x and y, there is no way of distinguishing them.

However, the test x == y no longer determines whether x and y are members of
the same equivalence class.  Indeed, there is no built-in test that will
determine whether x and y are in the same equivalence class.

Finally:

	x = [3, 4]
	y = [3, 4]

Now x and y are *not* in the same equivalence class, because changing one of
them doesn't change the other.  Nevertheless, x == y yields True.


So what I think is the interesting use case is this:  You evaluate two
expressions, for which the implementation is permitted to return the same
object twice or two distinct members of the same equivalence class, or
perhaps two distinct objects.  How can one determine straightforwardly
whether those objects are members of the same equivalence class?

If the objects' value is (3, 4), you can use ==.

If the objects' value is (3, a), there's no really easy way to do it.

If the objects' value is [3, 4], you can use 'is'.

I think it would be useful to have an operation that is as easy to use as
'is' that would make the determination. 




More information about the Python-Dev mailing list