The purpose of this lab is to provide you with a brief introduction to OOP in Python, which you will encounter in programming heavily in the future. The Python object system is built around classes and instances. There's a lot to go over before we have practice problems in this lab, so please follow along in your Python file and try things out in the terminal; it's great practice!
The starter file can be found here.
Let's start by considering a hypothetical type of object we might like to create. Then, we will look at the example code and work through it step-by-step as we introduce relevant aspects of the object system. Next, you will expand our class on your own as practice. Finally, we will finish the lab off by having you implement your very own class!
Also, here's a list of important words that you'll come across throughout the lab. If you're ever confused about any word, this is the place to refer to!
-
Object: An object is a collection of data with its own methods, attributes, and identity.
-
Method: A function that an object has. For example, the Python Turtle object has the method forward that causes the sprite to move forward.
-
Attribute: Basically, a variable that belongs to an object. For example, a Turtle object's coordinates are its attributes.
-
Class: A class is the blueprint of an object; it is the precise definition of an object's methods and attributes.
-
Constructor: The constructor is the method that Python uses when it creates (instantiates) an object. Not all attributes of a class are defined immediately, the constructor lets you define an object's attributes when you actually create the object.
-
Instance: An instance is an object made from a specific class. For example, your friend's iPhone is basically an instance of the iPhone class.
-
Instantiation: The creation of an instance. This is when the constructor actually creates the object.