Dungeon Coder


The Crawler Class

Introducing Object Oriented Programming

It is often helpful when programming to engage in what's called object oriented programming (OOP for short). In OOP, things are organized into classes, objects, and functions.

A class is a category of objects. For instance, there is a class to which I belong: humans. I am an object of the humans class. I also have some behaviors and states associated with myself as a member of the human class. I can speak (behavior), I can walk (behavior) , I am either speaking or not (state) , and I am either walking or not (state). I'm sure you can think of many other behaviors and states associated with being a member of the humans class.

Let's take a look at some object oriented programming in action in python:

Notice that class definitions start off similar to function definitions you saw in an earlier lesson. The difference you may have picked up on is the lack of () at the end of the first line that starts with def.

After the first line, you see a very important function that is called a constructor. Constructor functions allow us to create (hence, construct) objects belonging to a class. Inside the constructor class (which is always called __init__ (be mindful that init has both 2 underscores before and 2 underscores after)), we are able to specify some characteristics that belong to all objects of our class. In this case, I've only specified that all Crawler objects have a name. What is each Crawler's name? Well, that is what gets passed into the name parameter when we first construct a Crawler object.

Another strange thing you've likely noticed: self. The self keyword is used in python to refer to the object that is being used to call a function. In other words, whenever you see self within the class definition and its functions, realize that self ends up being whatever objects happen to make use of that function. It's important to know, as well, that functions that belong to a class can only be used by constructed objects that belong to that same class.

Okay, down below and outside of the Crawler class definition, you see one line that constructs a Crawler object (the line dm = Crawler("Dungeon Master")). The line after that calls the sayHi function on behalf of the dm Crawler object. The output results accordingly.

Though there is much more to recover, that is enough object oriented programming for now.


    You may need to refer to earlier lessons.

  1. How would the output change if you replaced the argument "Dungeon Master" with the argument "Crawler Apprentice"?
  2. Construct a new Crawler object called hero and pass the argument "Nemo Nil".
  3. Use hero to call the sayHi function. What is your output?
  4. Use a for loop to cause hero to call the sayHi function 10 times.
  5. Write a prompt asking the user to enter their Crawler's name. Then, create a new Crawler object called userCrawl with that user's input as the argument. Then, call the sayHi function on behalf of the userCrawl object. What is your output?

Join the Dungeon Crawl: A place for programmers to ponder, partake, and peruse postulations pertaining to programming, politics, potions, and pizza.

If you subscribe on Patreon, you will be granted access to the Dojo, a growing collection of quality computer science classes created and actively mantained and frequented by the Dungeon Master!

Become a Patron!


Contact: dungeon_master@dungeoncoder.com