Note that this page is under construction!

Lesson 5: More about functions, objects and methods

Subject: Objects. Parameters and return values.
New concepts: Mutable and immutable objects.
Work procedure: You are encouraged to discuss the work with others but you should always write your own code.

Try to solve the exercises before you look at the answers. Ask a teaching assistant if you don't understand the answers.

Estimated working time: 4 hours.
Examination: It is mandatory to present your solution to a teacher or assistant teacher according the time at the course home page.
This lesson will illustrate the concepts introduced in the previous lessons and will introduce some new concepts related to parameters and return values.

What is meant by an object?

We have used the term object mainly when we discussed lists and turtles. Objects have properties (e.g. the elements in the list and the position of the turtle).

Objects also have methods that can be used to manipulate the object. Examples of methods are append for lists and forward for turtles. Methods are functions bound to individual objects. We use the dot notation to to specify which object the method should be applied to.

For example, if we write t.forward(10) we mean that we want the turtle referred by t to move while u.forward(10) mean that we want the turtle referred by u to move.

Almost everything in Python are objects (integer numbers, floating point numbers, strings, functions, etc.). Since e.g. integer numbers are objects there are methods associated with them.

Try the following statements in the Python interpreter: (1.0).is_integer() and (1.5).is_integer(). Thus, we can see that the floating point numbers object in Python has a method called is_integer() that returns True if the number is an integer and returns False otherwise.

Objects can be either mutable or immutable.

Numbers and strings are example of immutable objects, while lists and turtles are examples of mutable objects.

Assignment is handled different for mutable and immutable objets. We will illustrate that with following code and figures.

 

 

im01.png

The figure to the right illustrates what occurs when the following code is executed.

t = turtle.Turtle() u = turtle.Turtle() x = 3 y = x

 

 

im02.png

 

 

 

If we then execute the following lines, the figure changes to

u = t y = 4

Since integer objects are immutable, y must receive a new object with the value of 4. If you could modify the value of the integer object, then x would also receive the value of 4, this will probably cause very undesired side effects...

After the assignment u = t, both u and t will refer to the same object. This means e.g. that t.forward(100) and u.forward(100) would both move the same turtle object 100 units forwards.

After the u = t assignment, no more reference exists to the red turtle. Even though it still exists in the program, there is no easy way to access it.

When an immutable object is assigned to a variable, a new object is created.

When a mutable object is assigned to a variable, the object reference stored in the variable is modified to point to the new object.

Arguments passed as parameters to a functions work in the same way ass assignments. Any "modification" to an immutable object that's used as a parameter in a function will not affect the object that was passed to the function as its argument.

On the other hand, if we use a mutable object, e.g. a turtle object, as an argument to a function, any modifications done to the parameter inside of the function will be observable outside of the function.

A revisit to the turtle pond

We will now take a closer look at functions that modify turtles. There are two different types of functions:
  1. A function that basically behaves like a method on the turtle object. It takes a turtle object as an argument, makes some modification to the object.
  2. A function that only use a turtle object as a tool to draw some geometric figure, like a circle or a square. Such function would not care which turtle object it uses to draw the figure. Thus, the function would not even need to be given a turtle object as an argument. Instead, the function could just create a turtle internally. In this way, a user of the function would not even need to know that the function uses a turtle to draw the figure!

Additional turtle methods that might be helpful in this lesson

Method Function
xcor() Returns the turtles x coordinate.
ycor() Returns the turtles y coordinate.
heading() Returns the heading of the turtle. 0 is along the x axis, 90 is along the y axis.
setheading(angle) Set the heading of the turtle.
towards(x, y) Returns the heading needed to point the turtle towards the point (xy).
towards(t) Returns the heading that would point the turtle towards the turtle referred to by t.
distance(x, y) Returns the distance between the turtle and the point (xy).
distance(t) Returns the distance between the turtle and the turtle referred to by t.

Turtles as parameters

If you want to move a turtle without drawing its path, you can use the following function:
def jump(t, x, y): t.penup() t.goto(x, y) t.pendown()

The function takes a turtle reference as a parameter, lifts its pen, moves the turtle to the (x, y) point and then puts its pen down again.

Turtles as the return value of a function

Example: make_turtle

The method turtle.Turtle() always creates new turtles at (0, 0). The following function creates a new turtle, moves it to the point (x, y) and returns the reference to the turtle to the caller of the function.

def make_turtle(x, y): t = turtle.Turtle() jump(t, x, y) # Use of the function defined above return t

Exercises

  1. Write a function rectangle(x, y, width, height, color) that creates a turtle and uses it to draw a rectangle with the width, height and fill color taken from the parameters of the function. The lower left corner of the rectangle should be at the point (x, y). Answer
    def rectangle(x, y, width, height, color):
        t = make_turtle(x, y)
        t.hideturtle()
        t.fillcolor(color)
        t.begin_fill()
        for dist in [width, height, width, height]:
            t.forward(dist)
            t.left(90)
        t.end_fill()
    
  2. La Tricolore

    Write a function tricolore(x, y, h) that draws the French flag with its lower left corner at the point (x, y) and the height h. The proportions of the flag should be 2:3.

     

    Answer
    def tricolore(x, y, h):
        w = h/2  	#The width of the color field
        rectangle(x, y, w, h, 'blue')
        rectangle(x+w, y, w, h, 'white')
        rectangle(x+2*w, y, w, h, 'red')
    
  3. pentagram

    Write a function pentagram(x, y, side) that draws a pentagram. The top of the pentagram should be placed at the point (x, y). The parameter side gives the length of the lines between the tips.

    Tip: The angle of the tips is 36 degrees.

     

    Answer
    def pentagram(x, y, side):
        t = make_turtle(x, y)
        t.hideturtle()
        t.setheading(270 - 36/2)
        for i in range(5):
            t.forward(side)
            t.left(180-36)
    

Assignment 1: Try the functions!

Uppgift 1

Put the above functions (jump, make_turtle, rectangle, tricolore and pentagram) in a Python file. Modify the pentagram function so that it takes a parameter for the fill color. Write code that produces the figure to the right.

Tips:

  1. If you give the turtle a speed of 0 (t.speed(0)) it will be quicker to draw!
  2. The pentagram has the odd coloring by "default".
  3. 3 lines of code is enough to draw the 10 pentagrams. Use for statements!

Note: The color filling of the pentagram is done differently in different version of Python, thus it's not incorrect if the pentagram is not color exactly the same as in the figure.

Default values of parameters

You can give parameters default values that are used if the argument is omitted when calling the function.

Example: If you declare the function rectangle this way:
def rectangle(t, x, y, width, height, color=''):
you can use the following function call:
rectangle(0, 0, 50, 50) This draws a square but does not fill it with any color.
rectangle(100, 100, 50, 50, 'blue') A square filled with the color blue.
rectangle(100, 100, 50, 50, color='red') A square with the filled with the color red. This is a good way to call functions if there are multiple parameters that has default values.

Exercises

  1. Add a parameter visible to the function make_turtle that has the default value of True. The call to make_turtle() should, like before, create a new visible turtle, while the function call make_turtle(visible=False) and makeTurtle(False) should create an invisible turtle. A invisible turtles speed should always be set to 0. Answer
    def make_turtle(x, y, visible = True):
        t = turtle.Turtle()
        if not visible:
            t.hideturtle()
            t.speed(0)
        jump(t, x, y)
        return t
    
  2. Write a function random_turtle(side) that creates a turtle and moves it to a random point inside a square. The square should be centered at the point (0, 0) and have a side length of side. The turtle should get a random heading between 0 and 359. The parameter side shall have a default value of 500. Answer
    def random_turtle(side=500):
        low = -side//2
        high = side//2
        t = make_turtle(random.randint(low, high),
                        random.randint(low, high))
        t.setheading(random.randint(0, 359))
        return t
    

Assignment 2: Paint flags

flag
Write a function vietnamese_flag(x, y, height) that draws the Vietnamese flag with its lower left corner at the point (x, y). The parameter height gives the height of the flag and the flag's proportions shall be 3:2. The pentagram shall be centered inside the flag. You can either approximate the size of the pentagram inside of the flag or you can look up the correct proportions on the internet.

Help: You will probably need to rewrite parts of the pentagram function since the pentagrams of the flag should not have any painted lines and the entire pentagram should be filled.

You may choose to draw any other flag that contains more than only rectangles.

Assignment 3: Dizzy turtles

  1. Create the file dizzyTurtles.py.
  2. Add the lines
    import turtle import random
    at the top of the file.
  3. Add the functions jump and rectangle.
  4. Write the function move_random(t) that moves the turtle in a random direction.

    The heading of the turtle shall be changed with a random value in the interval [-45, 45]. For example, if a turtle has the heading of 90, it's new heading should be in the interval [45, 135].

    When the new heading is set, the turtle shall move forward a distance randomly selected from the interval [0, 25].

    To create random numbers you can use the method random.randint(m, n). The method returns a random number that is greater than or equal to m and less than or equal to n.

  5. bild1

    Write a script that creates a turtle, draws a square centered on origin using the method rectangle. The square should have the side length of 500 and by filled with some color.

  6. Move the turtle (jump) to a random point inside of the square. See the figure to the right for reference.

  7. bild2

    Write a script that uses the for statement to make 250 calls to the move_random function.

    See the figure to the right for a reference.

  8. bild3

    Add code to your script so that the move_random function turns the turtle towards the origin if the turtle is outside the square.

    See the figure to the right for a reference.

  9. bild4

    Create a new turtle also at a random point. Give the turtles different colors using the color() method.

    Use the same for statement to move both turtles and change it so that move_random is called 500 times.

    If the distance between two turtles is less than 50 units, let one of them print the text 'close'. The turtle method write(string) writes a string in the window next to the turtle.

    The script should also count the number of times the two turtles are close to each other.

General Questions

  1. What datatypes have you seen in Python? Answer
    int, float, str, bool, list, tuple and turtle
  2. What is the difference between the expressions 2 and "2"? Answer
    The first has the data type of int and the second has the data type str.
  3. What is printed if you execute the statement print(int(1.8))? Answer
    Try it and explain the results!
  4. How can you find out which functions there are in the module math? Show! Answer
    dir(math)
  5. What is the difference between a function and a method? Answer
    A method is bound to an object while a function, normally, is not.
  6. What is a parameter? Answer
    A parameter is a variable that is declared in the definition of a function. When a function is called, the parameters are given the value of the arguments used to call the function.

Question

How many hours have you spent working on your computer in this course so far?

Go to the next lesson or go back.

Valid CSS!