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.
Objects are used to model real world or abstract concepts. We use attributes to model data and methods to model behaviors. But, how do methods actually work? How do objects get things done? That’s going to be our focus in this lesson.
We’re going to introduce some new concepts in this lesson. Don’t worry if they don’t all stick right away. Learning to program involves slowly building up the layers of required knowledge.
Let’s reestablish what we know about objects so far. Objects model concepts, and the name of the concept being modeled is referred to as an object’s type.
Attributes describe an aspect of the concept being modeled. For example if we were to model a cat, we might have an attribute for name, and weight, among others. Methods are used to model behavior. Which means they’re responsible for actually doing things.
Let’s start our review of methods with a definition. We’re going to revise this definition as we progress through the course. But, for now, I want you to think about methods as “where your code is going to live.”
It’s the code that lives inside the method that’s responsible for actually doing something.
A method’s code determines what the method does, and how it does it. But the code isn’t what we’re going to cover in this lesson. Don’t worry about what that code might look like. We’ll cover that in a later lesson. For now, just know that the way methods do things is with code.
So, if we’re not covering the code that determines what a method does and how, what are we covering? We’re going to cover the mechanics of methods. The goal here is to teach you how methods are used to model behaviors. Let’s use some visualizations to help with that.
Imagine we’re modeling a lemonade stand. We’re going to try and keep this one simple. We’ll use the three attributes. One to track how many lemons we have. One to track how much money we’ve made. And one to store the price per cup. Each of these is going to use the built in float type.
Let’s look at the behaviors we’re going to model for this lemonade stand. There are two methods, one for serving a cup, the other is used to determine if we’re losing money. We’ll get into the details of these in a bit. But, before we do that we need to better understand methods.
There are three things that methods can do that we haven’t covered.
Methods are able to accept input, return output, and interact with attributes.
What does it mean that methods can accept input? Sometimes before you can do something, you need some additional information first.
Imagine you’re in line at a lemonade stand and the person asks: how many cups would you like? That information, the number of cups that you’d like, is not something the lemonade stand employee could know until you give your answer. Sure, they could assume it’s one cup. But they don’t know until it’s time to serve.
Methods are a mechanism for modeling the behaviors of a concept. In this case we’re modeling the behavior of serving lemonade. However, since we don’t know in advance how many cups a person will purchase, we need a mechanism that enables us to provide that information when it’s time to serve.
In this example, our serve method needs to know how many cups to serve. So when modeling the serve method we would need to define input for the number of cups to serve. Methods can define zero or more inputs and the code inside the method can use the input data.
Let’s talk about how methods can return output. It’s common when modeling behaviors that you need to get some data out of an object. In the case of our lemonade stand, we have a method named calculate_loss. It calculates the amount of money lost to mistakes such as spilling a glass. In this case, we want the method to return the answer to the question: how much money was lost to mistakes?
That’s the data we want to get out of our lemonade stand object. Methods can return an object which other parts of our code can use. For example, our calculate_loss method returns a float that represents how much money was lost to mistakes.
Besides being able to accept input and return output, methods can also interact with attributes. What exactly does that mean? Our lemonade stand has attributes that represent the number of lemons, the amount of money made, and the price per cup. Everytime that we serve one cup of lemonade, we need to remove half of a lemon from inventory, and add the money to our account.
Because methods can interact with attributes, our serve method can make these changes. Our serve method can remove half of a lemon from inventory and add the money. It reads the price per cup attribute to determine how much money to add.
Being able to interact with attributes is the primary value of methods. Methods are a great mechanism for controlling the state of an object. In the case of our serve method we’re subtracting half of a lemon per cup served. So we’re using the serve method to control the state of our lemonade stand.
Methods being able to access attributes enables us to effectively model concepts. When we model a concept, we need to consider the behaviors of the concept. A behavior of our lemonade stand is that a cup can be served. We need to know what’s involved in serving a cup in order to model it. In our example, each cup of lemonade uses half a lemon and costs whatever amount is defined in the cost per cup.
This implies that when we serve a cup, we need to remove half a lemon from inventory. Because methods can interact with attributes, we can check to see if we have enough inventory to sell a cup. And if we do, we can subtract half a lemon from inventory.
So, methods are able to accept input, return output and interact with attributes. These traits are what enable methods to model a concept’s behaviors.
I mentioned that methods are “where your code is going to live,” It’s the code that will actually interact with attributes. In order to make that code run we have to call our method. What does that mean, to call a method?
Calling a method will run the method’s code. Call is the official term. For example I could say: I called the serve method and it functioned correctly. I’ll be using the term call often going forward.
If a method requires input then we need to provide that input when we call the method. Don’t worry about where that input comes from for now. We’ll cover that in future lessons. For now, just know that calling a method is how we run a method’s code. And when we call a method we need to provide required input.
Okay, I think we’ve hit a natural stopping point. Let’s summarize what we’ve covered. The way methods model behaviors is by accepting input, returning output and interacting with attributes. We know that method input allows us to provide data to the method when it’s called. Method output allows us to return data after a method is called. We also know that methods can interact with attributes. Which enables them to more closely model a concept’s behaviors by controlling an object’s own data. And a method is run by calling the method.
We’re slowly building a mental model of Python objects that will help provide context when learning the language. We’re still building up that understanding. Don’t worry if things don’t fully make sense now. It’ll start to fall into place.
For now, the key takeaways that you should carry into the next lesson are:
- Objects are a data modeling strategy which defines data using attributes and behaviors using methods.
- The concept an object models is referred to as its type.
- Python provides a lot of commonly required types built into the runtime.
- Attributes consist of names and values
- Methods can accept input, return output and interact with an object’s attributes.
- ...Calling a method is how the code within is run.
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.