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

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Jan 14 01:31:12 EST 2006


On Sat, 14 Jan 2006 04:22:53 +0000, Donn Cave wrote:

> Quoth Steven D'Aprano <steve at REMOVETHIScyber.com.au>:
> | 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?
> ...
> |> 2. What is the value of object()?
> 
> [ I assume you mean, the object returned by object(). ]
> 
> It doesn't really have a value.  I can't think of any kind of
> computation that could use this object directly.

Here is one:

obj_inst = object()

def computation(data):
    global obj_inst
    if data is obj_inst:
        print FirstOneThousandPrimes()
    else:
        print TextOfWarAndPeace()


It isn't a particularly useful computation, but it is a computation.



> |> 3. If two objects are equal with "==", does that
> |>   mean their values are the same?
> 
> Yes.
> 
> | >>> 3.0 == 3
> | True
> 
> Evidently the value of 3.0 is the same as the value of 3.

Okay.

>>> "3" == 3
False

Evidently the value of three is not the same as the value of three.

Let's try not to be too deep here, okay? Before asking "what is the value
of foo?", we have to agree on what we mean by "value". It is easy to tie
yourself into knots here.


> |> 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).
> 
> I think to be strictly accurate, attributes and their values may reside
> in a class instance, or in (one of) its class(es.)

When you call instance.attribute, attribute may be either a class
attribute or an instance attribute. If it is a class attribute, it belongs
to the class, not the instance, even if you access it through the
attribute. 
 

> In the most common
> case, functions will be in the class and data will be in the instance,
> but every variation on this is reasonably common.



> | ... 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.
> 
> For a given class and instance, it can be more obvious.  Take the object
> returned by object(), which has attributes like '__reduce__' - all those
> attributes may reasonably be considered "type" and not "value".

I don't see that "value" or "type" are opposite, I see that they are
orthogonal. The type of 1 is int, the value of 1 is integer one. The
type of object() is object, the value is itself. The type of
object().__reduce__ is method, and the value of object().__reduce__ is
the particular (generic) object bound to the name __reduce__ in the class
object namespace. That method is a class attribute, and so it part of the
value of object().


Am I the only one that wishes that object() had been given another name,
so it would be easier to talk about the generic programming concept object
and the specific Python new-style class object without confusion?


-- 
Steven.




More information about the Python-list mailing list