lecture 04 – classes. python has a number of classes built-in lists, dictionaries, sets, int,...

14
Lecture 04 – Classes COMPSCI 105 Principles of Computer Science

Upload: mckenna-bodge

Post on 02-Apr-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

Lecture 04 – Classes

COMPSCI 105Principles of Computer Science

Page 2: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

2COMPSCI 105 - Principles of Computer Science

Python has a number of classes built-in lists, dictionaries, sets, int, float, boolean, strings

We can define our own classes creates a new type of object in Python

Classes consist of: state variables (sometimes called instance variables) methods (functions that are linked to a particular instance of the class)

Classes

class name_of_the_class: definition of the class goes here

Page 3: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

3COMPSCI 105 - Principles of Computer Science

Pass is a statement that does nothing It is often used as a placeholder when developing code

The simplest class possible

class Point: pass

>>> p = Point()>>> p<__main__.Point object at 0x02702570>>>> p.x = 2>>> p.y = 4>>> p.x2>>> p.y4

Page 4: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

4COMPSCI 105 - Principles of Computer Science

Classes are designed to help build modular code Can be defined within a module that also contains application code Multiple classes can be defined in the same file

In this course, we will typically store each class in their own module To use the class in another module, you will need to import the module

Saving the class

class Point: pass Saved in a file called Geometry.py

from Geometry import Point

p = Point()Use the class like this

Page 5: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

5COMPSCI 105 - Principles of Computer Science

The object in memory

from Geometry import Point

p = Point()p.x = 5p.y = 7

px: 5

y: 7

Page 6: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

6COMPSCI 105 - Principles of Computer Science

We want to define the Point class so we can write code that sets the initial values of some variables

First, we need to define a special method of the Point class called a constructor The constructor is called whenever you create an object of the Point class.

Setting the initial state of the object

from Geometry import Point

p = Point(5, 7)

Page 7: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

7COMPSCI 105 - Principles of Computer Science

Each class should contain a constructor method Name of the method is __init__ The method always has at least one parameter, called self Self is a reference to the object that we are creating The constructor can have other parameters

Constructors

class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y

from Geometry import Point

p = Point(5, 7)print(p.x)print(p.y)

Page 8: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

8COMPSCI 105 - Principles of Computer Science

Defining more methods A method to shift a point by a given amount in horizontal and vertical directions

Note: the method is named normally, but has the additional parameter (self) as the first parameter All methods that are called on an instance of an object need the self parameter

Adding functionality

class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y

def translate(self, dx, dy): self.x += dx self.y += dy

Page 9: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

9COMPSCI 105 - Principles of Computer Science

Methods are defined to accept self as the first parameter

We call the method using: object_name.method(params)Method call is translated to: method(object_name, params)

Using the Point class

class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y

def translate(self, dx, dy): self.x += dx self.y += dy

from Geometry import Pointp = Point(0,0)p.translate(10,5)

Page 10: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

10COMPSCI 105 - Principles of Computer Science

Define a class that will be used to represent a square with a given side length. Your class should include a constructor that will allow the square to be used as follows:

Exercise

from Geometry import Square

side = 10s = Square(side)

Page 11: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

11COMPSCI 105 - Principles of Computer Science

Add a method to the class to calculate the perimeter of the square. The following code shows how the method may be used.

Exercise

from Geometry import Square

side = 10s = Square(side)p = s.perimeter()

Page 12: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

12COMPSCI 105 - Principles of Computer Science

Add a method to the class to change the size of the square using a scaling factor. For example, if you scale the square by a factor of 2, then the sides of the square will be twice as long. The following code shows how the method may be used.

Exercise

from Geometry import Square

side = 10s = Square(side)big_s = s.scale(2)

Page 13: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

13COMPSCI 105 - Principles of Computer Science

Add a method that compares the size of the square with the size of another square. The method should be called bigger_than() and should accept a square as a parameter

Exercise

from Geometry import Square

s = Square(6)t = Square(7)if s.bigger_than(t): print(“The first square is bigger”)

Page 14: Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes

14COMPSCI 105 - Principles of Computer Science

Classes are defined using the syntax:

Classes contain methods that define the behaviour and operations on objects of that class

We override the default behaviours for built-in methods/operations

Additional Resourceshttp://interactivepython.org/runestone/static/thinkcspy/Classes/fractions.html

Summary

class name_of_the_class: definition of the class goes here