30. Object-Oriented Programming:  "dot" notation

We are soon going to learn about a modern programming style called Object-Oriented Programming [OOP]. Python is an OOP language, although you could never have guessed from what we have seen up to now. Before we start writing object-oriented programs, 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. These are variables, like we have seen before except that they "belong" to Fido. 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:

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 add/remove robot

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')


# 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 if we write their name); 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
previousWhat's your name? - home - Globalization is a bad thingnext