Is 'everything' a refrence or isn't it?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jan 13 21:48:57 EST 2006


On Thu, 12 Jan 2006 16:11:53 -0800, rurpy wrote:

> It would help if you or someone would answer these
> five questions (with something more than "yes" or "no" :-)
> 
> 1. Do all objects have values?

All objects ARE values. Some values themselves are complex objects
which in turn contain other values, e.g. if I execute:

L = [None, 1, "hello"]

I have created a name 'L' which is bound to ("has the value of") a list
with three items. The first item has the value of ("is the object") None,
the second has the value of ("is the object") 1, and the third is the
string "hello".


> 2. What is the value of object()?

These seems complicated because we are using the same word ("object") for
both the generic programming term, the class (strictly type, although the
difference is not important), and the instance itself.

Let's make it less confusing by using a case where the three things are
different:

class Foo:
    pass

Foo is a class. Classes are (generic programming term) objects.
Foo() returns an instance of the Foo class. Instances are (generic
programming term) objects.

The value of Foo() is the instance. Why? int(data) returns an instance of
int, and str(data) returns an instance of str. It makes sense to say that
the value of the int instance 1 is one, the value of str instance "abc" is
the string "abc", and so forth. So we'd like to talk the same way about
more complex objects: the value of an instance is itself.

Hence the value of Foo() is the Foo instance at some particular memory
location. In this specific case, Foo() creates an object with very little
behaviour and and no distinguishing characteristics except their location
in memory, where as ints like 1 2 and 7 are easily distinguished.

Now let's return to (non-generic) object(). object is (as well as the
generic programming term) a class (strictly type, but the difference is
unimportant). object() returns an instance of class object:

>>> object()
<object object at 0xf705e3b8>

So the value of object() is some particular instance (of type object).



> 3. If two objects are equal with "==", does that
>   mean their values are the same?

Intuitively yes, but not necessarily:

>>> 3.0 == 3
True

(We want the float 3.0 to be equal to the int 3, but we can distinguish
between them by something more than their location in memory.)

You can define a custom class:

class EqualAll:
    def __eq__(self, other):
        return True

but that's a pathological case.



> 4. Are object attributes part of an object's type
>   or it's value, or something else?  (I think the first.)

The type/class defines what attributes (generic) objects have. The value
of the attribute lives with the instance itself (that's why fred.attribute
and barney.attribute can be different). Whether you want to say the
attribute itself is part of the class or part of the instance value is, in
my opinion, not a useful question. The answer depends on which way you
want to look at it.



> 5. The (only?) way to get an object's value is to
>   evaluate something (a name or a "reference"(*)
>   that refers to the object.

I can't think of any other way to get at an object except by accessing
that object, or even what it would mean if there was such a way.


-- 
Steven.




More information about the Python-list mailing list