Lecture 17 — Classes, Part 2

Overview

  • Review of classes
  • TimeOfDay class
  • Lab 5 revision
  • Techniques that we will see:
    • Calling class methods from within the class
    • Class objects storing other objects, such as lists
    • Lists of class objects

Review of Classes

We will use our Point2d class from Lecture 17 to review the following:

  • Attributes:
    • These store the data associated with each class instance.
    • They are usually defined inside the class to create a common set of attributes across all class instances.
  • Initialization: function __init__ called when the object is created.
    • Should assign initial values to all attributes
  • Methods
    • Each includes the object, often referred to as self, as the first argument.
    • Some change the object, some create new objects
  • Special methods start with two underscores. Python interprets their use in a variety of distinct ways:
    • __init__ is the initializer
    • __str__ is the string conversion function
    • __add__, __sub__, etc. become operators

TimeOfDay Class

  • Listed in the Lecture 18 notes, but we have not gotten to it yet.
  • Access information about hours, minutes and seconds
  • AM or PM
  • Convert to string
  • Addition of times
  • Difference between two times.
  • Many other methods.
  • Only attribute is the number of seconds since midnight.
    • This is not known to the code that uses the TimeOfDay class.
    • It is done for programming convenience, as we will discuss in lecture.
    • Example of encapsulation!

Exercises

We will work through the implementation details together during lecture. The skeleton implementation is attached to these notes

Larger Example — Restaurant Class

Recall Lab 5:

  • Read and parse input lines that look like:

    The Greek House|42.73|-73.69|27 3rd St+Troy, NY 12180|\
       http://www.yelp.com/biz/the-greek-house-troy|Greek|1|5|4|5|4|4|5|5|5|5|5|4
  • Find restaurants that satisfy certain criteria: distance, rating, type.

  • Original implementation based on a list was awkward:

    • We had to remember the role of each index of the list — 0 was the name, 1 was the lattitude, etc.
  • New implementation here is based on a class

Start to a Solution, the Main Code

Let’s look at restaurant_sol.py

  • Posted on the Piazza and attached to the back of the handout.
  • Imports lat_long_to_dist.py:
    • Converts the lattitude/longitude values for two restaurants into a distance between them.
  • Function convert_input_to_restaurant
    • Parses a restaurant line
    • Creates and returns a restaurant object
  • Function build_restaurant_list
    • Opens the input file
    • Reads each line
    • Calls convert_input_to_restaurant, and appends the resulting restaurant to the back of a list
  • Main code:
    • Builds the restaurant list
    • Gets the position, distance, rating, and restaurant type information from the user.
    • Goes through the restaurants to find those that match the criteria. This code is not yet written.

Exercises

Looking at the code that is in restaurant_sol.py

  1. Outline the methods that must be in the Restaurant class, including the parameters that must be passed to each method.
    • Thus, the way we want to use the class is dictating how we write it.
  2. Implement the rest of the main code assuming these methods are implemented correctly.
  3. (A slightly different topic:) In several places throughout the code, restaurants and lists of restaurants are being copied. Why is this not a substantial waste of computation?

Turning to the Restaurant Class

Look at rest.py

  • Again, this is posted on the Piazza site and attached to these notes as well.
  • The exact set of methods is likely to be somewhat different from what you came up with for the exercises just completed.
  • The __init__ function specifies the attributes.
    • Others could be added, such as the average rating, but instead these are computed as needed by methods.
    • Importantly, each class object stores a list of ratings, illustrating the fact that classes can store data structures such as lists, sets, and dictionaries.
  • We will briefly go over the remaining functions during lecture.

Exercises

  1. Write the average_rating, min_rating and max_rating methods. These should return -1 if the list is empty.
  2. Add code to the end of rest.py to test the functions you have added.

Discussion

  • What is not in the Restaurant class?
    • No input or line parsing. Often, we don’t want the class tied to the particular form of the input.
    • As an alternative, we could add a method for each of several different forms of input.
  • Often it is hard to make the decision about what should be inside and what should be outside the class.
    • For example, we chose to have a method that tells us if a restaurant is in a particular city, but we could have written a different method that returns that name of the city and made the comparison outside the class.
  • The Restaurant object stores a list as an attribute
    • This is our first example where the attributes are more than just simple variables.
  • We could add an Address class:
    • Reuse for objects other than restaurants
    • Not needed in this (relatively) short example.
    • Without it we store the address as a string; this feels like an incomplete implementation.

Summary

  • Review of the main components of a Python class:
    • Attributes
    • Methods
    • Special methods with names starting and ending with __
      • Initializer method is most important
  • Important uses of Python classes that we have seen today:
    • Calling class methods from other class methods
    • Classes containing lists
    • Lists of class objects.
  • Design of Python classes
    • Start by outlining how they are to be used
    • Leads to design of methods
    • Specification of attributes and implementation of methods comes last