[Edu-sig] Introduction to object and "dot" notation: feedback wanted

Andre Roberge andre.roberge at gmail.com
Wed Jan 18 21:28:29 CET 2006

Hi Everyone,

I am busy writing lessons for rur-ple (I noticed it's been kindly
mentioned here recently) and thought, in spite of the "flack"
following the request received by some of you for a book review ;-) ,
that I would ask for feedback about an introduction I have written to
OOP and the "dot" notation.

[I am *not* planning to ask feedbacks on all the lessons ;-)]

Context 1. The set-up of rur-ple is a world in which a robot can be
made to accomplish certain tasks.  Initially, the "function-based"
approach is used to instruct the robot to do its stuff.  For example,
a simple program might be as follows:

if next_to_a_beeper(): pick_beeper

Context 2.  The preceding lessons would have covered python data
types, variables, functions, and most python keywords, all used in the
context of the robot world or at the interpreter.

And now... here's the lesson which you are welcome to trash,
criticize, shoot down in flames or ignore!

P.S.  html formatting rudely removed from what follows...

__Object-Oriented Programming:  "dot" notation__

We are going to learn about a modern programming style called
Object-Oriented Programming [OOP]. Before we start writing, we will
first learn how to read and understand the notation used.


__All in a dog's day__

Fido is a dog. During a typical day, he does various actions: he eats,
runs, sleeps, etc. Here's how an object-oriented programmer might
write this.

Fido = Dog()

In addition, Fido has various qualities or attributes. He is tall (for
a dog) and his hair is black. Here's how the programmer might write
the same things.

Fido.size = "tall"
Fido.hair_colour = "black"

In the object-oriented language, we have the following:

= Dog is an example of a class (of objects).
= Fido is an instance (or particular object) in the Dog class.
= eats(), runs() and sleeps() are methods of the Dog class; 'methods'
are essentially like 'functions' which we saw before (the only
difference is that they belong in a given class/instance).
= size and hair_colour are attributes of a given instance/object
= Objects can also have other objects that belong to them, each with
their own methods or attributes:

Fido.tail.type = "bushy"

We'll see how this works later. For now, let's see how Reeborg can use
the "dot" notation.


__A used robot get his name__

So far, all the programs we wrote instructing Reeborg to accomplish
tasks have been written without using the Object-Oriented Programming
(OOP) notation. Let's start with a simple example.

First, we start by having an empty world, removing the robot if needed
by pressing the add/remove robot button

Now, you might remember that RUR in RUR-PLE stands for: Roberge's
*Used Robot*; the robots we use are old and faulty. [We will learn how
to fix them later.] So, we will create our first instance of the
UsedRobot class and name it, appropriately, Reeborg! We will then
instruct it to take one step and then turn itself off.

Reeborg = UsedRobot()

Try it!


__More robots__

Just like functions can have arguments, methods can too. Start with an
empty world and try the following:

# add robot at origin [by default], but with more interesting colour
Larry = UsedRobot(colour='blue')
# second robot, default colour (grey) facing North
Curly = UsedRobot(1, 3, 'N')
#  Third robot carries beepers
Moe = UsedRobot(1, 2, beepers=9, colour='yellow')

.for i in range(3):
.    Larry.move()
.    Curly.move()
.    Moe.move()

# Turning off any one robot ends the program

Robots come in a variety of colours: grey (by default), yellow, blue,
light blue, green and purple. They can be positioned anywhere in the
world (with more than one robot at the same intersection), face any of
the four directions ('E' [by default], 'N', 'S', 'W') or carry a
number of beepers from the start. Note that there are two named
arguments (beepers and colour) and three unnamed ones (street, avenue
and orientation). The two named arguments must appear last (their
order may be interchanged); the three unnamed arguments, if they
appear, must appear in the same order. Thus, if we want to specify an
orientation (say 'N'), we must first specify a street and an avenue so
that the orientation is the third argument.

The following are allowed declarations:

R1 = UsedRobot(2) # created at 2nd street, 1st avenue, facing East
R2 = UsedRobot(2, 3) # 2nd street, 3rd avenue, facing East
R3 = UsedRobot(3, 1, 'S') # 3rd steet, 1st avenue, facing South
R4 = UsedRobot(5, colour='yellow') # 5th street, 1st avenue, facing East

The following declarations are not allowed:

R5 = UsedRobot(3, 'S') # orientation is not 3rd argument
R6 = UsedRobot(colour='yellow', 5) # unnamed argument listed after named one

========= end of lesson ==========

More information about the Edu-sig mailing list