Week 4: Session 1

Classes, Objects, Modules, Packages and Libraries

Activity 1: Review Week 3

Input/Output

File I/O

JSON

Internet API's

Activity 2: Demonstrate where Classes and Objects are used in the Game

Activity 3: Intro to Classes and Objects/Modules and libraries

As programs grow, it becomes important to organize and encapsulate both data and functionality. We are always looking for ways to share code in way that allows us to just write the code once and not rely on copy/paste to share it.

Objects are nouns and can be described by their parts, or characteristics (adjectives) and what they can do (verbs)

Classes allow us to define code in the way we would define an object in real life.

The class code is a template for the object, and when we want to use the object we get an instance of a specific object.

Modules are code chunks such as classes, stand alone functions and constants that are stored in a file and can be imported into another file. Look at the code for the python os module: https://github.com/python/cpython/blob/master/Lib/os.pyarrow-up-right

Packages are a collection of Modules. It is a directory with a collection of files and an __init__.py file which distinguishes it from just a directory with a bunch of python scripts. We think of the directory as a namespace which means I can have a file called update.py in a directory that is a package named "Student" and Student.update would be different than a update.py not in the package. Namespacing allows us to reuse names for files.

Libraries are containers for packages. Look at the code for python's numPi library: https://github.com/numpy/numpy/tree/master/numpyarrow-up-right

Exercise:

Let's define a car class

characteristics (nouns and adjectives): make, model, color, year, max speed

what they can doe (verbs): start, stop, brake, move forward, move in reverse, park

Activity 4: Code a class

Code a car object using Python language elements shown below

class This defines the name of the object template

self This is the variable that the object uses to refer to itself in code

def This is how you define a data or method

__init__ This is code that runs when we want to create a new instance of the class which is an object

return Some methods can return data as we learned with functions

Exercise:

Get the car object code running and add a function to accellerate (increase speed). Use the code below to start.

Activity 5: Code for instantiating an Object from a Class

First we write function (main) that instantiates the class to create a specific object. Then we call the function (main()) to execute it.

Exercise

Code a main function that gets an instance of the car class. Only the start method is called in the code in main.

Activity 6: Modules

Modules help us to organize code into a separate files. Using the import command we can access module code from another file.

The import command names the file (without the .py extension) that contains the code you want to import. You can reference functions in that file by using the dot(.).

Exercise

Create a file (name_prompt.py) that contains a function that asks you your name.

Create a file (use_name_prompt.py) that import the first file and executes the function in it.

Here's an example of two files (name_prompt.py and use_name_prompt.py) that are working together using the import command.

Activity 7: Install and Import Libraries

Libraries are made available by package managers like pip. We'll look at libraries we've used and try out some new libraries.

Last updated