The road to mastering the Python programming language is paved with objects. Objects are Python’s foundational building block. The entire Python programming language is used to create and control objects. This course provides a high-level glimpse into the basic mechanics of objects. With the goal of providing you with a vocabulary and mental model for understanding Python.
Upon completing this course you’ll be familiar with the anatomy and behaviors of objects.
This course was designed for first-time developers wanting to learn Python. Existing developers already familiar with the concept of an object may want to skip.
This is an introductory course and doesn’t require any prior programming knowledge.
There’s a phrase that I’ve used more than a few times. I keep saying: Objects are a data modeling strategy which defines data using attributes and behaviors using methods.
I’ve also expressed that everything in Python is an object. Objects are Python’s core building block. And we’ve visualized a few different types so far. We’ve seen a cat object and a lemonade stand. We’ve also seen Python’s built-in string, integer, and float types.
We’ve hit a point where it’s time to start putting what we’ve been covering into practice. In this lesson, we’re going to start modeling a basic concept. We’ll talk through the process of modeling it and at the end of the lesson, we’re going to have a conceptual version of our object. In an upcoming lesson, we’ll compare our new conceptual model to the actual Python code used to create it.
The concept we’re going to model is that of a numeric counter. I want to share why I chose this example before we model it out. I chose this example because it’s conceptually simple. With only one attribute and one method, it doesn’t do much.
However, it does demonstrate how to read and change an attribute from a method. Since we’re going to implement this in code later in the course, it needs to stay simple.
Okay, with that said, let’s review the concept that we’re going to model.
The concept is a numeric Counter. Here’s our only specification: The purpose of the counter is to increment a number by one every time an event occurs.
Let’s break that down into attributes and behaviors. Looking at this sentence, I notice two things.
- The phrase: counter to increment a number by one tells me that we need an attribute that references an integer
- The phrase every time an event occurs tells me that we need a method to model the event that causes the number to increment.
Let’s start by naming the concept we’re modeling. This will determine our object’s type. The better we understand the concept that we’re modeling, the better our object can become. So before we write even a line of code, it’s important to think about the concepts you’re attempting to model. We’re going to call our new type: Counter.
Let’s talk about attributes for our Counter type. We know that we are going to increment a number by one every time some event occurs. We’re going to add an attribute to our counter named number which is set to zero by default.
We’re going to require a method that can be called every time some event occurs. What exactly does that mean, when some event occurs? Our limited specification indicates that when some event occurs, increment by one.
We don’t need to know what that event is, because we know how to model behaviors using methods. As long as we have a method that when called will increment the number, we’ve met our specification. We’re going to name our method count.
Okay, so we’ve defined our Counter type. We have an attribute named number that will store an integer. We have a method named count that when called will increment the number attribute by one. So if we call the method 5 times, the number attribute will reflect 5.
That’s our complete concept. A counter that will increment a number by one when a method is called.
With this Counter concept defined, let’s wrap up here. Your key takeaways for this lesson should be that:
- The purpose of the counter is to increment a number by one every time an event occurs
- When modeled in code our counter object will have an attribute named number and a method named count.
That’s going to wrap up this lesson. Thanks so much for watching and I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.