[Edu-sig] Python for Beginners

kirby urner kirby.urner at gmail.com
Sun May 25 21:17:00 CEST 2008


On Sun, May 25, 2008 at 10:58 AM, John Posner <jjposner at snet.net> wrote:

> 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.

Right, got it.  But my beginning math teachers get to operator overloading
within the first 20 minutes, because we're using Python to motivate some
insights in elementary group theory, what we're using to replace overbuilt
sections of calculus in some flagship charter high schools.  I spell it all out
in my Chicago talk (Pycon 2008 channel on YouTube).

Just last Saturday we were overloading __add__ in my Dog class, having
some "Dog %s loves %s" % (self, other) syntax going.  These were
beginners of another type (not my gnu math teachers), but there's
really nothing mysterious about __ribs__ if you've gone dir(8) since
day one (which we obviously have, given my interactive, projected
approach).  So yes, it took a lot more than 20 minutes in this case
(wrote up each session in my blog, in case others want to steal my
ideas).

>
>> 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.

Right, as long as by "zero names" you're ruling out any pointers
to pointers through a data structure, which are anonymous from
another angle (the structure itself has a name, but the objects
may be like Rod(color="Red"), with color choosing the length
behind the scenes. d =  {"red":Rod()} is then a string literal mapped
to an object, *like* a top-level name, but not.  d is the top-level
name in this namespace.  Rod( ) in a sense has no name, is
a call to a constructor that returned on object captured within
a dictionary.  So it won't be garbage collected, as it knows
there's this pointer for some reason (for what reason the object
will have no clue).

>> 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.

Not sure if you're confusing semantic model with implementation model
(the latter frequently uninteresting to practicing biologists).

> 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

I'd agree, except before x learns y's secret, only y knows it.  x = y is
not a symmetric situation.  y on the right "knows something" that x is
"now learning", after which point in the program "x now knows".  So
I think it's OK to have this idea of y whispering something to x, then
a parting of ways.

Kirby


More information about the Edu-sig mailing list