Value vs Identity (was Re: Pass variable by reference)

Chris Angelico rosuav at gmail.com
Wed May 7 22:02:05 EDT 2014


On Thu, May 8, 2014 at 7:22 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Mark H Harris <harrismh777 at gmail.com>:
>
>> A == B
>> True
>>
>> A is B
>> False
>>
>> [...]
>>
>> This is just one of a dozen 'different' kinds of examples. And the
>> answer is the same, Python does not have variables, Python has names
>> bound to objects.
>
> That is a different topic and isn't related to variables at all.
> Instead, you are talking about object identity:
>
>   >>> 2 * 3000 == 6000
>   True
>   >>> 2 * 3000 is 6000
>   False
>   >>> "abc"[:1] == "a"
>   True
>   >>> "abc"[:1] is "a"
>   False
>
> But hey, we can open another thread for whether Python has values or
> objects!

Python has objects. Objects have both identity and value. This is true
of every language I can think of that has any sort of pointer type; a
few examples:

1) A C string is a pointer-to-char. You compare string identity as
pointer value, and string value with strcmp(). Same with anything you
malloc(); its value is what's in the memory, its identity is the
pointer value (or memory location).

2) A REXX string, under the covers, is implemented as a C string, so
the same applies. But that's cheating a little, as there's no way in
REXX itself to probe identity; all you can work with is value.

3) Pike strings are always interned, so their identities and values
will always correspond, but arrays, mappings, and other refcounted
pointer types are compared for identity with == and for value with
equal().

4) Python objects always have identities, which are compared with
'is', and values, which are compared with '=='.

5) JavaScript objects are always compared by identity, and there are
numerous posts on StackOverflow etc about how to compare value. But
strings, AFAICT, are always compared by value.

6) A tuple in a database (okay, this is pushing the "language" bit a
little) ought, in a good design, be able to be identified and compared
exclusively by value. But since good design is not 100% prevalent,
every table row must have an identity as well. In PostgreSQL, this can
be done with OIDs and such; in BTrieve, the file position is a
fundamental attribute of the record.

7) BASIC has strings in memory, just like C does; using VARPTR and
PEEK, you can find that address, and use it as the string's identity.
You can then use LSET or RSET to mutate it without changing its
identity, or reassign it to do a Python-style rebinding. I would put a
caveat here that it's inadvisable, except that... well, it's BASIC, so
writing any code at all is inadvisable.

8) PHP objects have separate identity and value, although AFAIK
arrays, strings, and integers don't.

So, this isn't really a Python question at all. It's more like trying
to figure out which is the evil twin (pro tip: he's the one sticking a
dagger into you [1]), where the two are indistinguishable (value) but
are distinct (identity).

ChrisA

[1] http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=229965



More information about the Python-list mailing list