About the implementation of del in Python 3

Nathan Ernst nathan.ernst at gmail.com
Thu Jul 6 14:56:12 EDT 2017


In Python, "==" is not a reference equality operator (and I hate Java for
their misuse of the operator), so I absolutely disagree with using the Java
description to describe Python's "==" operator, primarily because, well,
it's wrong. Simple example:

With Python 3.5.2 (should hold for any version 2.4 or greater):
>>> a = 1
>>> b = 1
>>> a == b
True
>>> a is b
True
>>> c = 1000
>>> d = 1000
>>> c == d
True
>>> c is d
False

The "==" operator is testing for equality or equivalence. The "is" operator
is testing for identity. Are these 2 things/names/references the same
*instance*.?  Python internalizes small integers (IIRC, the range [0, 100]
is internalized).  Although integers in Python are immutable, they're not
always the same *instance*.

Hence, identity and equality/equivalence are separate. Identity checking in
Python (or at least CPython) is faster than equivalence (can test the
pointer to the underlying CPython object), but may lead to unexpected
results. One of the reasons you really only see the "is" operator used in
production code is to test against None, because None is a read-only
singleton.

On Thu, Jul 6, 2017 at 11:28 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:

> On Thu, Jul 6, 2017 at 9:41 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> > As a good example of the style I'm looking for, take a look at:
> >
> >    <URL: https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html>
>
> Java reference types have basically the same concept of identity as
> Python objects, so I dug around to find what definition Java uses.
> This is what I came up with:
>
> """
> There may be many references to the same object. Most objects have
> state, stored in the fields of objects that are instances of classes
> or in the variables that are the components of an array object. If two
> variables contain references to the same object, the state of the
> object can be modified using one variable's reference to the object,
> and then the altered state can be observed through the reference in
> the other variable.
> """
>
> Also, under the reference equality operator:
>
> """
> At run time, the result of == is true if the operand values are both
> null or both refer to the same object or array; otherwise, the result
> is false.
> The result of != is false if the operand values are both null or both
> refer to the same object or array; otherwise, the result is true.
> """
>
> If that language were used for Python, would it suffice for you?
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list