object types, mutable or not?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed May 16 02:39:52 EDT 2018


On Wed, 16 May 2018 11:30:26 +1000, Ben Finney wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> 
>> On Tue, 15 May 2018 07:29:39 -0700, Mike McClain wrote:
>>
>> > I had gotten the impression that everything in OOP is an object but
>> > you're all saying that variables are not objects.
>>
>> Perhaps it is better to say every VALUE in Python is an object.
> 
> IMO that is better than using the term “variable”, which carries baggage
> from other languages when people try learning Python.
> 
> But not good enough. It was you, Steven (I think?) who taught me that
> using the term “value” interchangeably with “object” is problematic.

Not guilty Yer Honour! It wasn't me!


> That's because, in Python an object can remain the same object, while
> its value changes.

Indeed. But something can BE a value and HAVE a value at the same time, 
just as a class can be both a class and an instance (of the metaclass) at 
the same time.

It is true that if we consider mutable objects like lists, then the 
list's value can change as we move items in and out of the list.

But regardless of the value OF the list, we can still describe the list 
itself AS a value.

English is not optimal for discussing these concepts -- possibly no 
language is, or can be, as the concepts of identity, sameness, value etc 
are fuzzy and nebulous. If I replace the handle of my grandfather's axe, 
and then a year later I replace the head, is it still my grandfather's 
axe?

https://en.wikipedia.org/wiki/Ship_of_Theseus

Interesting if not relevant:

https://www.sciencedirect.com/science/article/pii/S1074552113001154


But I digress... we ought to distinguish between the value of a list, 
which of course may change through the list's lifetime, and the fact that 
no matter what value it has, it is still a value.

I'm not the same person I was 40 years ago, but both then and now I 
remain a person.


[...]
> Therefore, an object is not its value (otherwise, when the value
> changes, we necessarily have a different object. That's false, and so
> the equivalence is also false.)

I think you are begging the question by assuming that a change of value 
implies a new object. That's not how the words are used in standard 
English.

My house can appreciate and depreciate in value, even if I do nothing but 
occasionally remember to take out the trash... but we surely must agree 
that it is the same house, even if I replace the furniture, replace a 
broken window, repaint the living room, replace a broken tap... 


> An object is not a value; an object *has* a value. The object retains
> its identity even when its value changes.

Here you have hit on the crux of the matter. Why cannot both statements 
be true?

Webster's 1913 edition of "value" lists 10 distinct meanings for the 
noun; WordNet gives 6; the Oxford English dictionary on my desk also has 
6, and if I count sub-definitions (1a, 1b, 1c etc) I get fifteen.

Looking at the WordNet definitions, the one I think is nearest to the use 
I am making is something between numbers 1 and 4:

    1: a numerical quantity measured or assigned or computed;

    4: relative darkness or lightness of a color; "I establish the
       colors and principal values by organizing the painting into
       three values--dark, medium...and light"-Joe Hing Lowe

Obviously I'm not talking about the darkness or lightness of a colour 
specifically, but if we can enumerate three values "dark", "light" etc, 
we can surely enumerate a larger number of values including:

    None, True, False, various strings, lists, tuples, etc...

Some of these have numerical values in the sense of 1. above, some have 
textual values, some might be considered enumerations, some are compound 
values containing other values...


>> Variables *hold* values, or if you prefer, names refer to values. But
>> since they aren't themselves values, names/variables aren't objects.
> 
> I really want that sentence to be useful for this pedagogical purpose.
> My earlier message in this thread went to some length to do something
> similar.

The distinction I wanted to make, but forgot to, was that certain things 
are *first-class values* in a language, and others are not.

In Python, all objects are first-class values, and all values are 
objects. That's not the case for all languages, where some values are not 
first-class.

https://en.wikipedia.org/wiki/First-class_citizen

But not all *things* are values at all. For loops, expressions, names, 
try...except clauses etc are "things" but they're not values at all.


> But because we only invite later confusion when the “a value is an
> object” false equivalence needs un-learning, I can't let it pass.

And I wouldn't expect anything less from you :-)

If we are looking for a single, short, plain English sentence which 
perfectly explains Python's object model using nothing but ordinary words 
whose meaning are self-evidently true and completely unambiguous, I fear 
we are doomed to be disappointed.

But it is fun to keep trying.



-- 
Steve




More information about the Python-list mailing list