tough-to-explain Python

Ben Finney ben+python at benfinney.id.au
Tue Jul 7 20:06:07 EDT 2009


kj <no.email at please.post> writes:

> In <mailman.2796.1246997332.8015.python-list at python.org> Chris Rebert <clp2 at rebertia.com> writes:
> 
> >You might find the following helpful (partially):
> >http://effbot.org/zone/call-by-object.htm
> 
> Extremely helpful.  Thanks!  (I learned more from it than someone
> who will teach the stuff would care to admit...)

I have got very good results from teaching using the analogy of “paper
tags tied to physical objects” to describe Python's references to
values.

The analogy allows illustration of many otherwise confusing facts:

  * an assignment is the act of putting a tag onto an object

  * a tag leads to exactly one object, and it can be re-tied to a
    different object

  * an object can have arbitrarily many tags on it

  * containers (e.g. lists, dicts, sets, etc.) don't contain objects,
    they contain tags leading to objects

  * passing arguments to a function always makes new tags leading to the
    objects passed in, and throws those new tags away once the function
    returns

  * etc.

The analogy can then be contrasted to how Python *doesn't* do it: named
boxes with one value per box. You can point out that many other
languages do use this model, so they should be aware of it, but Python
doesn't use it.

> I had not realized how *profoundly* different the meaning of the
> "=" in Python's
> 
>   spam = ham

Access the object referenced by ‘ham’, and assigns the reference ‘spam’
to that object.

> is from the "=" in its
> 
>   spam[3] = ham[3]

Access the object referenced by ‘ham[3]’, and assigns the reference
‘spam[3]’ to that object.

No, they're exactly the same. The only thing that's different is the
references you use; the assignment operates *exactly* the same in both
cases.

-- 
 \     “We are no more free to believe whatever we want about God than |
  `\         we are free to adopt unjustified beliefs about science or |
_o__)              history […].” —Sam Harris, _The End of Faith_, 2004 |
Ben Finney



More information about the Python-list mailing list