Explanation of list reference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 17 01:21:23 EST 2014


On Mon, 17 Feb 2014 11:54:45 +1300, Gregory Ewing wrote:

> Chris Angelico wrote:
>> Because everything in Python is an object, and objects always are
>> handled by their references.
> 
> <beginner_thought> So, we have objects... and we have references to
> objects... but everything is an object... so does that mean references
> are objects too? </beginner_thought>

Every *thing* is an object. References aren't *things*. An analogy: there 
is no thing "Greg Ewing" which is independent of you, there is you, the 
thing with an independent existence, and then there is the reference (or 
name), "Greg Ewing", a label for you.

But perhaps it is better to say that all *values* are objects.


> This is the kind of trouble you get into when you make a statement of
> the form "everything is an X"[1]. When we say "everything is an object",
> we don't literally mean everything, only... well, those things that
> *are* objects. Which doesn't really help the beginner much.

I don't believe that in Python there are any *things* (values) which 
aren't objects. Even classes and exceptions are values. That's not the 
case in all languages. In Java, you have objects, and you have unboxed 
(native) types, and you have classes which aren't values at all. (Or at 
least, not first-class values.)


So before you ask: for-loops aren't things (values). Neither are while-
loops, try...except blocks, pass statements, del statements, etc. Nor are 
names and other references -- I believe that there is a practice in 
certain areas of computer science to call them "l-values", which despite 
the name are not values at all -- at least not in Python. Apart from 
binding and unbinding:

    ref = something()
    del ref

which are somewhat special, you can't perform computations on 
*references*, only on the things (values) which references refer to.

Contrast this to a hypothetical language which allowed you do perform 
computations on references, say using a special ! operator:

    # given
    x = 23
    XY = 42
    # then
    print (!x+"Y")

would print 42. The expression !x+"Y" operates on the reference itself, 
and then print operates on the value referred to by that new reference.


> [1] Mathematicians tried this. "Everything is a set!" Yeah, right...

No, that's okay. You only get into trouble when you have self-referential 
sets, like "the set of all sets that don't contain themselves". 



-- 
Steven



More information about the Python-list mailing list