Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71069

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

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'attribute': 0.07; 'variables': 0.07; 'string': 0.09; 'integers': 0.09; 'postgresql,': 0.09; 'record.': 0.09; 'type;': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; '"a"': 0.16; "'is',": 0.16; 'caveat': 0.16; 'distinct': 0.16; 'examples:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'identities': 0.16; 'identity:': 0.16; 'mappings,': 0.16; 'objects.': 0.16; 'subject:variable': 0.16; 'tuple': 0.16; 'twin': 0.16; 'variables,': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'thu,': 0.19; 'value.': 0.19; '>>>': 0.22; 'memory': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'pointer': 0.24; 'string,': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'compare': 0.26; 'posts': 0.26; 'values': 0.27; '[1]': 0.29; 'am,': 0.29; 'topic': 0.29; 'related': 0.29; 'compared': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'evil': 0.31; 'file': 0.32; 'figure': 0.32; 'another': 0.32; 'open': 0.33; 'implemented': 0.33; 'table': 0.34; 'subject: (': 0.35; 'basic': 0.35; 'except': 0.35; 'etc': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; "he's": 0.36; 'done': 0.36; 'changing': 0.37; 'so,': 0.37; 'two': 0.37; 'anything': 0.39; 'does': 0.39; 'itself': 0.39; 'how': 0.40; 'design,': 0.64; 'more': 0.64; 'different': 0.65; 'talking': 0.65; 'here': 0.66; '3000': 0.68; 'hey,': 0.75; 'address,': 0.75; '100%': 0.77; 'cheating': 0.84; 'examples.': 0.84; 'identity;': 0.84; 'pike': 0.84; 'subject:Value': 0.84; 'dozen': 0.91; 'probe': 0.91; 'same,': 0.91; 'to:none': 0.92; 'numerous': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:cc:content-type; bh=jmizzQryyFnevDnqi+Wp+ncawRt3EyqZoczy79SC05c=; b=I/xdlW83+BifFVOMZbJhj5zo+peJ9iW39/yaEbuXqRctzhGN4SjkZbjXvllq7m6Yj1 +9+z7j1aMJP8jKVeYJuxpp/ftiNQJ96WutbGylG7Su3qyqKgEyqKVtrLJOKHEteZHP4k CVMPufai51zxVXxVAaCkqXtHOw2nIA+mNJBsY1vF54ZpqFnNjl8JB9Qkwi9xkxfQnSln aAbh+gpSFUELWizb9W+aSH3Wod7WqOQnNnl6TlfVFXg/u+T2a/Zg5Q2Oijw/Loq5s8I0 Wx5zg+Lrq60BKCyq4ySGPLb/+XA2eTbZKBwcDV3OtC1/G8xBq1IJe8K7YWr3r28Gk49C N3eQ==
MIME-Version 1.0
X-Received by 10.58.207.74 with SMTP id lu10mr476911vec.15.1399514525875; Wed, 07 May 2014 19:02:05 -0700 (PDT)
Date Thu, 8 May 2014 12:02:05 +1000
Subject Value vs Identity (was Re: Pass variable by reference)
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9756.1399514528.18130.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1399514528 news.xs4all.nl 2854 [2001:888:2000:d::a6]:43788
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71069

Show key headers only | View raw


On Thu, May 8, 2014 at 7:22 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Mark H Harris <harrismh777@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Value vs Identity (was Re: Pass variable by reference) Chris Angelico <rosuav@gmail.com> - 2014-05-08 12:02 +1000

csiph-web