You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "is" operator has well defined semantics. It compares
object identity. Allowing it to be redefined would a terrible
idea, IMHO.
mathematician commented on Feb 18, 2002
mathematicianmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=118203
You can say the same for all the operators in python. The
default behavior would be object identity, but:
x is y
is the same as doing
id(x)==id(y)
So the 'is' operator is actually superfluous except for
its readability value.
Your comment seems to me like a knee jerk resistance to
change. Now if you were to tell me that it would make
python drastically slower or that it would be difficult to
implement, then you would have a good point...
I'm afraid I agree with Neil that this would be a
disaster. There's code that absolutely depends on "is"
meaning object identity. One example (you'll find others
if you just look for them): _deepcopy_tuple() in the
standard copy.py relies on it, in its second loop. If the
operator ever "lied" about object identity, the semantics
of deep copies could break in amazing ways. There's lots
of "foundational" code in a similar boat (e.g., my own
Cyclops.py replies on current "is" semantics all over the
place, and that's an important example because it's not in
the standard distribution: we have no way to locate, let
alone repair, all the code that would break).
If you want to pursue this, then because it's not backward
compatible, it will require a PEP to propose the change and
introduce a corresponding __future__ statement.
The other thing you'll get resistance on is that "is" is
dirt cheap today, and some code relies on that too. If it
has to look for an object override, what's currently an
exceptionally fast implementation:
case PyCmp_IS:
case PyCmp_IS_NOT:
res = (v == w);
if (op == (int) PyCmp_IS_NOT)
res = !res;
break;
will at least have to do new indirection dances too through
the type objects (to see first whether either operand
overrides "is").
mathematician commented on Feb 19, 2002
mathematicianmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=118203
what about:
x is y -> id(x)==id(y) or x.__is__(y)
than old code would not break & one could use is for more
than just object identity equivalence. Of course if the
two operands are the same object, then it always returns
true.
I would rather see
ifdbrowisempty:
# do something
than
ifdbrow.isEmpty():
# do something
which is like java's string equivalency test
strvar.isequal("to another string").
This way an object could 'be' anything :) Hey, well maybe
for Python 3000. If so, I also recommend adding an
operator called 'is a' which is equivalent to isinstance()
in current python.
is true whenever "x is y" is true today, but may be true
even in cases where "x is y" is false today. If so, it's
not backward compatible, and general code relying on
current semantics would still break. For example, any kind
of general code that's crawling over an object graph needs
to know whether it's seen an object before, current "is"
can and is used to answer that question precisely, and it's
as bad to tell it that two distinct objects are identical
as it is to tell it that two identical objects are
distinct. The standard copy.deepcopy() is one example
of "general code that's crawling over an object graph".
OO languages with object identity really need a way to ask
about object identity, and "is" has always been that way in
Python (btw, "is" existed long before "id()" was
introduced). For that reason, if you write a PEP, I think
you'd get farther by leaving "is" alone and proposing
another spelling instead.
Activity
mathematician commentedon Feb 18, 2002
Being able to overload the 'is' operator would lead
to nicer more readable code:
'is not' could just test __is__ and return 'not
retval'
nascheme commentedon Feb 18, 2002
Logged In: YES
user_id=35752
The "is" operator has well defined semantics. It compares
object identity. Allowing it to be redefined would a terrible
idea, IMHO.
mathematician commentedon Feb 18, 2002
Logged In: YES
user_id=118203
You can say the same for all the operators in python. The
default behavior would be object identity, but:
x is y
is the same as doing
id(x)==id(y)
So the 'is' operator is actually superfluous except for
its readability value.
Your comment seems to me like a knee jerk resistance to
change. Now if you were to tell me that it would make
python drastically slower or that it would be difficult to
implement, then you would have a good point...
tim-one commentedon Feb 19, 2002
Logged In: YES
user_id=31435
I'm afraid I agree with Neil that this would be a
disaster. There's code that absolutely depends on "is"
meaning object identity. One example (you'll find others
if you just look for them): _deepcopy_tuple() in the
standard copy.py relies on it, in its second loop. If the
operator ever "lied" about object identity, the semantics
of deep copies could break in amazing ways. There's lots
of "foundational" code in a similar boat (e.g., my own
Cyclops.py replies on current "is" semantics all over the
place, and that's an important example because it's not in
the standard distribution: we have no way to locate, let
alone repair, all the code that would break).
If you want to pursue this, then because it's not backward
compatible, it will require a PEP to propose the change and
introduce a corresponding __future__ statement.
The other thing you'll get resistance on is that "is" is
dirt cheap today, and some code relies on that too. If it
has to look for an object override, what's currently an
exceptionally fast implementation:
will at least have to do new indirection dances too through
the type objects (to see first whether either operand
overrides "is").
mathematician commentedon Feb 19, 2002
Logged In: YES
user_id=118203
what about:
x is y -> id(x)==id(y) or x.__is__(y)
than old code would not break & one could use is for more
than just object identity equivalence. Of course if the
two operands are the same object, then it always returns
true.
I would rather see
than
which is like java's string equivalency test
strvar.isequal("to another string").
This way an object could 'be' anything :) Hey, well maybe
for Python 3000. If so, I also recommend adding an
operator called 'is a' which is equivalent to isinstance()
in current python.
tim-one commentedon Feb 20, 2002
Logged In: YES
user_id=31435
Unless I'm missing something intended,
x is y -> id(x)==id(y) or x.__is__(y)
is true whenever "x is y" is true today, but may be true
even in cases where "x is y" is false today. If so, it's
not backward compatible, and general code relying on
current semantics would still break. For example, any kind
of general code that's crawling over an object graph needs
to know whether it's seen an object before, current "is"
can and is used to answer that question precisely, and it's
as bad to tell it that two distinct objects are identical
as it is to tell it that two identical objects are
distinct. The standard copy.deepcopy() is one example
of "general code that's crawling over an object graph".
OO languages with object identity really need a way to ask
about object identity, and "is" has always been that way in
Python (btw, "is" existed long before "id()" was
introduced). For that reason, if you write a PEP, I think
you'd get farther by leaving "is" alone and proposing
another spelling instead.
nascheme commentedon May 23, 2002
Logged In: YES
user_id=35752
__eql__? :-P