[Edu-sig] spinning 'self'

John Zelle john.zelle at wartburg.edu
Tue Aug 14 02:33:15 CEST 2007


On Monday 13 August 2007 3:43 pm, kirby urner wrote:
> Python is remarkably liberal in its design, in that 'self' is not a
> keyword.  You're free to use other names for this important
> place in memory.
>
> Yet there's still self-imposed discipline (another way of saying
> 'self discipline'), such that we tend to all use 'self' anyway, by
> convention (but Python-the-interpreter doesn't complain if we
> don't -- not likely to change).
>
> However, as Python teachers have widely remarked, this concept
> of 'self' is not easily communicated at first, given the OO paradigm
> is itself still coming into focus for some students.

The nice thing about explicit 'self' from a teaching perspective is that it 
really is not different from other parameters. Once students understand 
parameters (not necesarily an easy thing for them), self does not really 
involve any OO mystery at all. It's just a matter of showing how the 
parameters match up in a method call:

obj.someMethod(actual1, actual2)            someMethod(self, formal1, formal2)

(you'll have to draw the arrows yourself, my font is proportional -- rats!, 
there should be a nice arrow from obj to self, actual1 to formal1 and actual2 
to formal2)

Then of course you explain the reason for the special syntax is that obj is 
what determines where we go looking for the definition of someMethod. The 
actual result is still just a plain old function call.

> In the class definition, the 'self' is really a 'place holder' (akin to
> zero) i.e. it doesn't do any work, but keeps a place open, like a driver's
> seat waiting for a driver.
>
> When an *object* of said class is created, the self becomes
> specified, as a kind of *anchor*.  And plumbing the depths of
> 'self' is easy:  all you need is __dict__ (i.e. a self is basically
> another namespace, a concept already familiar from earlier
> discussions).

I would quibble about self not doing any work (you need it to access its 
attributes), but I understand the point you are making. Again, though this 
makes things seem a bit more mysterious than they really are. The self 
parameter is a placeholder in the exact same sense that all formal parameters 
are place holders for objects that are provided at call time. The self 
parameter has no 'specialness' in this regard.

> Python-by-comic-book (including on the net) is an emerging
> genre, influenced by Head First, Dive Into, and For Dummies
> series.
>
> I'm storyboarding for comics (could be TV shows) in which
> ' this ', ' me ' and ' ghost ' each get a crack at occupying
> the ' self ' position.  Each of these English options has its
> own logic and connotations.
>
> I especially like ' ghost ' because of its ' ghost in the shell '
> connotations (a link to manga, the genre we're in).[1]
>
> As Python goes towards Unicode, other "non-Romanji"
> substitutes for ' self ' may suggest themselves.
>
> However (back to self-discipline) I think it's wise to stick
> to the guidelines and go with non-idiosyncratic forms.

Yes, otherwise you incur the wrath of the Python Gods. Deviation from self 
appears to be  the one form of heresy that is never tolerated under any 
circumstance. "To thine self be true!"

> David Goodger was good on this in his 'Idiomatic Python'
> at OSCON 2007 [2]:
>
> If your object is an iterable (i.e. has a "duck face" (which is
> like being a "duck type" but sounds more like "interface"))
> then address as such, as in in 'for myvar in self.__dict__',
> and not using has_key() or some such more specialcase
> form.

Huh? I'm affraid I don't follow this. If my object (meaning self?) is 
iterable, I doubt that I intend to iterate through its namespace.  If the 
point is simply not to use keys() as the iterable sequence for a dictionary, 
whether it's self.__dict__ or any other dictionary,  that I get.

> In this same spirit (aha! another one), we just go with 'self'
> at the end of the day.
>

And all is right with the world.

--John

--
John M. Zelle, Ph.D.             Wartburg College
Professor of Computer Science    Waverly, IA     
john.zelle at wartburg.edu          (319) 352-8360  


More information about the Edu-sig mailing list