object types, mutable or not?

Ben Finney ben+python at benfinney.id.au
Tue May 15 13:22:18 EDT 2018


Mike McClain <mike.junk.46 at att.net> writes:

> Many thanks to those teachers who responded.

Thank you for asking the question in a way that allows the discussion
:-)

> I think I got it.
> The variable is not the object just as the name is not the thing.

Yes. The term “variable” is so overloaded, for people new to Python,
that I prefer to avoid it altogether. I discuss Python's assignment
behaviour in terms of binding and references.

> I had gotten the impression that everything in OOP is an object

That is definitely not true. There are many languages that support OOP
where *not* everything is an object.

See <URL:https://en.wikipedia.org/wiki/Object-oriented_programming#OOP_languages.

> but you're all saying that variables are not objects.

Yes. In the “everything is an object” adage, it's important to realise
what ontology is being used — what “things” exist.

It would be more accurate to say “every value is an object”, but that
gets into how “value” has technical meanings and is IMO less helpful
than “everything is an object”.

A different formulation to make it more accurate would be “everything
that can be addressed by an expression is an object”. Not as catchy, and
I don't expect to see it used as often :-)

The point being, there are many things *we* can talk about which don't
“exist” as things in the context of that adage. Numbers, file handles,
data structures all are “things” and therefore are objects in Python.

Variables, sequence indexes, memory locations, names, statements are
concepts with meaning when *talking about* Python, but are not “things”
that have a value in a Python expression. Because they are not “things”
in Python, those are not objects in Python.

The adage is intended (IIUC) to contrast with languages other than
Python that *do* support OOP, but where *not* everything is an object.
Examples include Java, PHP, Pascal.

> Does a variable have a type?

No, because in Python, “variable” refers to the *binding* between a
reference and an object. In that binding, only the object has a type,
and its type is totally unaffected by whatever references are bound to
that object.

> If so what is the type of a variable and how is that demonstrated
> if 'type()' reports what the variable points to?

This is an illustration of why I don't find “variable” a helpful term
for discussing Python behaviour.

Instead, an expression like ‘type(foo)’ is better understood as getting
the type of object that the name ‘foo’ references.

It doesn't make sense to ask “what is the type of foo” if you're
thinking of ‘foo’ the name. The type is not a property of the name and
not a property of the name–object binding (the “variable”). The type is
a property of the object.

-- 
 \         “It is the fundamental duty of the citizen to resist and to |
  `\          restrain the violence of the state.” —Noam Chomsky, 1971 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list