[Edu-sig] Python for Beginners

John Posner jjposner at snet.net
Sun May 25 19:58:24 CEST 2008


Comments on Kirby's comments ...

> Except we also have "x == y" and "x is y" in Python, with "x 
> == y" mapping to a __rib__ in x (optionally absent in the 
> more general case), i.e.
> x == y calls x.__eq__(y)  
> http://docs.python.org/ref/customization.html
> 
> This is worth mentioning as that leaves the door open to "==" 
> meaning something quite alien, in the hands of a perverse 
> programmer, whereas "=" (assignment) and "is" (memory id 
> comparison) are much more hard-wired.

I wouldn't mention the fact that the "==" operator can be "corrupted" by an
incorrect/mischievous definition of the __eq__ method, any more than I'd
mention that "*" can get similar treatment with a definition of __mul__.
This is *way* beyond Python for Beginners, IMHO.


> Deallocation of memory objects is more rightly the job of the 
> garbage collector.  "del y" impacts the referenced PyObject 
> in that the latter's name counter goes down.

Yup, "del y" operates on the name (remove the sticky note from the object,
then throw away the sticky note). The important thing for beginners to know
is that if an object has zero names (sticky notes), there's no way to refer
to it. It's as good as gone. The fact that some background process might
delete the no-name object from memory is not something beginners would, or
should, care about.


> When we go x = y, x and y dock for a sec, and a payload goes 
> from y to x, like an URL, or a remote, a controller.
> 
> The copy is likewise disposable, and y will not ask for it 
> back, or shouldn't have to, because y keeps its own copy, 
> it's own relationship with that snake-like thing with 
> __ribs__ (the number 8 for example).

Hmmm ... "x and y dock for a sec" could lead a beginner to believe that "y"
refers to "x", rather than "y" refers to the same object that "x" refers to.

I have nothing against Internet-oriented metaphors, as long as they help the
beginner to learn the way in which the language *actually* works. How about
this:

  On the Web, you can buy a product directly from the manufacturer, using
the
  manufacturer's product code. Or you can buy it from Amazon, which has its
own
  product code. There's only one product, but it can have two (or more)
identifiers
  that enable you to access it.


> But x and y have no lasting relationship.  For a split 
> second, they're coupled, in that x needs a copy of y's URL, 
> the pointer to the object.  Then they go their separate ways 
> *immediately*.

I'd rather say that the names "x" and "y" have no relationship at all. Each
of them happens to have a relationship with the same object.

-John Posner



More information about the Edu-sig mailing list