[Tutor] Copying a mutable

Alan Gauld alan.gauld at btinternet.com
Thu Jun 9 00:05:40 CEST 2011


"Válas Péter" <sulinet at postafiok.hu> wrote

> > really care if the two names are bound to same object, or just to 
> > two
> > objects that happen to have the same value.

> Being one of the purposes of Python to be a simple educational 
> language, I
> want to make this simple to a beginner who does care. :-)

That's good. But the fundamental precursor to explaining it is to
explain the concept of an object. Because everything in Python
is an object. If you don;t explain the concept of an object the
beginner will never understand the different behaviours.

There is nothing unusual in this, other object oriented languages
require the same fundamental comprehension of objects.
(eg Smalltalk - alsdo a language developed to teach
beginners - indeed children - about programming.)

> My question is: how would you explain the different
> behaviour of a list and a tuple for a beginner?

By explaining what immutability means and that some
objects are mutable (the objects are changed in place)
and others are not (new objects of the same type but
with different values are created) - and that's just how
it is...

Every language has some core concepts that need to
be addressed early in the learning experience. (Like
the idea of maxint in C and Pascal say...)  Python's
data model and concept of mutability is one such
concept.

> This is another point the beginner may be confused:
> multiple assignments.

Frankly I never use those with beginners.
They are syntactic sugar and never needed.
If a beginner comes across them somewhere then
I explain them. But they are an idiom best picked
up by osmosis IMHO.

> As far as I understand, assignment means giving a value to
> a variable which is the expression used by classical languages

and in math where the terms originated.

> variables (such as Pascal or Basic). Python has no variables,

Python does have variables but the implementation model
is different to languages like C and Pascal where they map
to memory locations. But compared to Lisp and Smalltalk
(both heavily used as teaching languages) Python is semantically
very similar.

> since even the simpliest data is an object,
> but we still say assignment,

Exactly, provided we have explained the concept of objects
and that everything is an object, then assignment is simply
the  associating of a name with an object - a very simple
and consistent concept for a true beginner. It's the folks who
come with a load of mental baggage from other, memory
mapped, languages that have the problems because they
are trying to force Python's model onto their preconceived
notions of variables. If you come from a clean slate with no
previous knowledge Python's model is simple and consistent.

> In this sense, if I say, "assignment" is a subset of "binding",

No because binding has a specific meaning in relation to
classes and object instances. Assignment in Python is a
form of name association not binding.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list