What other languages use the same data model as Python?

Ben Finney ben+python at benfinney.id.au
Wed May 4 17:43:59 EDT 2011


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> Given the following statement of Python code:
>
> >>> x = "spam"
>
> what is the value of the variable x?

Mu (無).

‘x’ is a name. Names are bound to values. Talk of “variable” only
confuses the issue because of the baggage carried with that term.

Yes, the Python documentation disagrees with me.

> (1) The string "spam".

> I argue that any answer except for (1) is (almost always) counter-
> productive: it adds more confusion than shedding light.

I prefer to respond “The name ‘x’ is bound to a string, "spam"”.

(If I knew which version of Python we're using, I'd qualify the type as
“a text string” or “a byte string”.)

> It requires thinking at the wrong level, at the implementation level
> instead of the level of Python code. If we define "value" to mean the
> invisible, inaccessible reference, then that leaves no word to
> describe was the string "spam" is.

Fine, the value is the string. No problem there.

But the data model of Python doesn't fit well with the ideas that the
term “variable” connotes for most programmers: a box, perhaps of a rigid
shape (data type) or not, which is labelled ‘x’ and nothing else. For
another variable to have an equal value, that value needs to be copied
and put in a separate box; or perhaps some special reference to the
original needs to be made and placed in a box.

Saying “variable” and “has the value” just invites baggage needlessly,
and creates many assumptions about Python's data model which has to be
un-done, often after much false mental scaffolding has been built on
them by the newbie and needs to be dismantled carefully.

As we all know, Python doesn't work as the term “variable” implies for
many. Rather, ‘x’ isn't a container at all, but an identifier only. It's
more like a paper tag which can be tied to objects; that brings a bunch
of useful implications:

* that the paper tag is tied to only one object

* that a paper tag tied to no object is rather useless

* that many paper tags can be tied to the same object

* that the paper tag is only loosely associated with the object and can
  be removed and tied to a different object, without any change to the
  objects themselves

* that the object doesn't necessarily have any tag at a given point in
  time

* that the tag with its string is useful to find the object even without
  a name on the tag (the concept of other non-name bindings to objects,
  e.g. list items)

All those implications of the “paper tag” analogy are helpful in
thinking about the Python data model. And those implications don't even
have to be explicitly stated in order to help.

So instead of inviting confusion with “variable ‘x’ has the value
"spam"” I prefer to say “name ‘x’ is bound to the value "spam"”.

> The intellectual contortions that some people will go through to
> hammer the square peg of actual programming language behaviour into
> the two round holes of "pass by value" and "pass by reference" never
> cease to astonish me.

I maintain that avoiding the use of the term “variable”, and gently
correcting those who use it in the context of Python (with humility in
the face of the fact that the Python documentation liberally uses the
term), can short-circuit a lot of that needless confusion.

Python isn't pass by anything. Nothing gets copied, nothing gets passed;
when a function is called with an object as a parameter, the object
stays put, and simply gets a new temporary name bound to it for the
function's use.

Speaking of objects (or values) with names bound to them helps that
explanation in a way that the traditional image of “variables” does not.

> > Whatever, a rose by any other name...)
>
> Do you really think that roses would be the symbol of romantic love if 
> they were called "disgusting stink-weeds of perversion and death"?

Juliet's point stands, though: they would still smell as sweet, and the
term you describe would be unlikely to catch on since it doesn't
describe them well at all.

-- 
 \      “I like to fill my bathtub up with water, then turn the shower |
  `\       on and pretend I'm in a submarine that's been hit.” —Steven |
_o__)                                                           Wright |
Ben Finney



More information about the Python-list mailing list