Class 17: Very light Object-Oriented Python

This is the last Python we'll do in this course, after this we move to Matlab. We will only talk about this lightly but the next course in this series is all about Object Oriented Thinking (CISC 181).

What and Why?

So Object-Oriented Programming is a philosophy centered in modular thinking. It's really about making code versions of things which only let outsiders access certain things. The first example I ever understood was a microwave:

A Microwave is a very complex device with heating elements and radiation and other technical innards. But to use it I just need to know how to press some very simple buttons (and a handle). A normal user of a microwave doesn't need to know which wires to connect and how to activate the rotation motor. That doesn't mean those things aren't needed, they can just be kept out of the way.

Here is a first, kinda dumb, class:

The class definition makes a template for objects (sometimes called instances). You can use the class as a little object producer. The class definition will run those commands when you call it (Blueprint() in this case). But the output value will be an object which has "attributes" that match the variable names.

In this case, everytime we run Blueprint() we'll produce a new building which has the same number of stories and same material.

In reality different objects have different attributes. Here is a simple class which captures a person that might eat at UDairy:

This class defined two functions, one special function called __init__ which gets executed whenever any new object is created. The other is a method that allows this person to do an action.

You'll notice that all class methods have a special first argument which is the object itself. I called mine self in my functions which is a Python convention (but not a requirement).

Cat Task 1: Build a cat class which stores breed and name. Write a noise method that prints a cat noise.

Age Task 2: Make a different person class that passes in birth year as an opening attribute. Give this class a method, age_in_year(self, current_year) which displays the persons age in that year. Create yourself and your mom, then see how old you'll both be in 2030.

Python Built-ins

Python has many method names that it will honor as implementations of normal things. We saw __init__(self), but now you can also use something like __add__(self, other) so that Python knows how to add your things.

Another cool built-in is __repr__(self) which tells Python what to print.

Print Task: Adjust the above snippet so that print(andy+sara) displays baby. (Implement __repr__(self) to return a representation of a person). What does print(andy) do?

I invite you to take a look at all of Python's "magic methods".

Creating a World

One thing that is fun about OOP (object oriented programming) is creating armies of objects that become your world. Many will be of the same type but with different individual attributes. Here is a familiar example:

Animated version (I couldn't get that black arrow out of the way):

Random Color Task 3: Take the tree class and have each separate tree have a randomized color (from some sensible set of colors) for a base and for leaves.

Independent agents. When it comes to simulations and virtual worlds, it's nice to have your objects move around as individual entities. Even in simulations of real things.

Coin Flipping Task 4: Make a class Coin with a sideup attribute and a flip method. Have flip be random and the value of sideup either "heads" or "tails" (initial values can all be heads). Simulate a box of 200 coins. Flip them all and count the "heads" (remove the "tails"). Repeat until the box is empty. How many iterations did it take? Run this experiment several times.

Here is a visual "independent agent" OOP:

Trace Task 5: Run that several times. What makes the trees move independently?

So wiping the screen and redrawing is a valuable paradigm. Here is a turtle only game (not by me). Their big notion is to have each tile be a turtle represented by an image. Then when the game state updates because of a key press, redraw all of the turtles:

Here is a pygame example of classes (made by a friend of mine), it needs you to save this image as swarmer.png:

Daily Challenge / In-Class Mini-Project

In one of the visually oriented libraries (pygame, kivy, or turtle) make a class which will randomly instantiate as either a cat or a dog. Have the art be an image file from the interenet. Have the animal move around the screen (randomly, tile by tile, or loop, whatever you can imagine working). You can work in your groups.