[Edu-sig] Python Pedagogy
Andre Roberge
andre.roberge at gmail.com
Fri Jul 21 12:46:05 CEST 2006
On 7/21/06, kirby urner <kirby.urner at gmail.com> wrote:
> Although we all agree to use 'self' as the "me object" proxy, it's not
> a keyword, and we could use a different stand in, e.g.:
>
> class Human (object):
>
> def __init__(me, name):
> me.name = name
>
> def __repr__(me):
> return 'Hi, my name is %s' % me.name
>
> >>> import subgenius
> >>> aguy = Human('Bob')
> >>> aguy
> 'Hi, my name is Bob'
The "me" approach is very anthropomorphic and may not work well in all
languages. Mind you, the same is true for "self".
I do something similar with rur-ple (but have not had feedback
on this so far). First, to set the stage.
I introduce the concept of variables extremely
early one (well before they are used) by talking about synonyms,
a term everyone should be familiars with.
Here's how (from lesson 5).
====
While the creators of Reeborg designed him so that he obeys
instructions in English, they realised that not everyone understands
English. So, they gave him the ability to easily learn a second
language. For example, if we want to tell someone to "move forward" in
French, we would say "avance". We can tell Reeborg that "avance" is a
synonym of "move" simply by writing
avance = move.
The order here is important; the known command has to be on the right,
and the new one has to be on the left. Note that we don't have any
parentheses "()" appearing since the parentheses would tell Reeborg
that we want him to obey an instruction; here, we are simply teaching
him a new word. When we want Reeborg to follow the new instruction, we
will use avance().
===================
Note that I don't really use variables in the usual way until lesson 27.
Now, on to "self". (from lesson 37)
-------
class RepairedRobot(UsedRobot):
def turn_right(synonym_used_to_refer_to_this_object):
for i in range(3):
synonym_used_to_refer_to_this_object.turn_left()
[some explanation here....; note that the long term is normally
highlighted in red for clarity.]
Python creates a new instance of the class RepairedRobot and defines
all the methods, replacing the first argument of the method
(synonym_used_to_refer_to_this_object) by the name of the instance
(newReeborg).
Now, synonym_used_to_refer_to_this_object is rather a long name to
type. By convention, another variable name is used: self. Thus, to
follow the normal convention, I should have written:
class RepairedRobot(UsedRobot):
def turn_right(self):
for i in range(3):
self.turn_left()
=======================================
I think it's easier to accept using "self" rather than
"synonym_used_to_refer_to_this_object" as a convention, as it is much
shorter to type. Using "self" instead of "me" may be harder to
justify to a rebellious teenager who is looking for ways to
distinguish herself.
André
More information about the Edu-sig
mailing list