The course is part of this learning path
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.
When I first learned to program with Python it wasn't my first programming language. So, when learning it, I always compared it to what I already knew. That did help me to learn the Python syntax more quickly. Though, it took years before I understood Python well enough to wish I had learned it differently.
In hind-sight had I understood Python's data model from the start, it would have given me a better foundation for truly understanding Python. More generically than understanding Python's data model; I want you to really focus on learning what Python is and how it functions, from its own perspective. As opposed to trying to understand it through the lens of something similar.
So, if you already have some familiarity with another programming language, I urge you to try not to make too many comparisons until the end of this course. When you look closely at Python's underlying architecture you start to notice that Python is designed to make it easy for developers to work with data.
I think that's the most succinct way that I can currently describe Python. With that premise in mind -- that Python is designed to make it easier to work with data -- let's take a glimpse into the concept on which Python is built.
Python is an application written in the C programming language, and it runs on all kinds of different operating systems including Windows, Linux, macOS, and others. When you run the Python application you're starting up the Python runtime. The runtime defines all the rules that make Python, Python. There are a lot of interesting details regarding the implementation of the runtime. However, we're going to focus on just the aspects you need to know for now.
The Python runtime reads Python code and turns it into Python objects. Objects are one of Python's foundational building blocks. Objects are a mechanism for modeling code after real world and abstract concepts. Objects model a concept by defining the attributes and behaviors which best describe the concept we're trying to model.
That’s all a bit abstract, so let’s explore objects through an example. Let's imagine what it might look like to model a house cat. To do that we have to determine which attributes matter to us based on how we plan to use that data. Since we don’t have a real world use case, let’s just focus on some basic attributes and behaviors that might define a house cat. Our cat object might have attributes for name, age, breed, and weight. And its behaviors might include methods of playing and meowing.
So, when I say "objects model real world or abstract concepts," it means that objects describe meaningful attributes and behaviors of the concept being modeled. The entire goal of modeling concepts and behaviors with objects is to make it easier for developers to work with data. When code models concepts that we’re familiar with, it’s easier to make assumptions about how to interact with that model.
Python considers both data and behaviors to be attributes. However, behaviors are a little different than data attributes because they can do things. We'll talk more about how they do things in other lessons, for now, just knowing objects can do things is enough. Because of this difference between data and behaviors, when developers refer to attributes, they're most likely referring to attributes representing data and not the behaviors.
Attributes which model behaviors are called methods. As in, objects have methods of doing things. So, Python objects are made up of attributes and methods. Alright, objects are used to model real world and abstract concepts. The concept being modeled is referred to as an object’s type. Every object that exists was created to model some concept. The name of the concept describes what the object can do. Python refers to the name of the concept being modeled as the object’s type. For example the Cat object models the concept of house cat. So Python would consider this object’s type to be: Cat. If we were to model a list, then the object’s type would be: list.
Modeling a cat is an example of a real world concept because we can see and touch a cat. What about abstract concepts such as numbers, lists, and text? What might those look like to be modeled? And why would we want to? If we model numbers that behave just like the numbers we use in daily life, then we can add, subtract, multiply, divide, and do everything else that numbers can do. If we were to model a list we would expect to be able to add and remove items from the list. In the same way we would remove an item from a todo list once completed. The point here is that if we can model a concept, then we can interact with the resulting objects in an intuitive way.
Recall that types are the name given to the concept being modeled. Which means the more types that are available to us, the more things we can do. Which is why Python provides many built-in types. Which we'll cover in other lessons.
Objects range in complexity from objects which only store data, to massive objects with many attributes and methods. However, conceptually they're all the same; an object is a collection of attributes and methods. And the object's type is the name for the concept the object models.Let's revisit our house-cat object so that we can dive a bit deeper into Python objects. Our cat object has attributes for name, age, breed, and weight and methods for playing and meowing.
Let's ignore the methods for now and focus on the attributes. Notice there are three different types of data used in these attributes. The name is just some text, as is the breed. The age is a number, and the weight is also a number, though, this type allows for decimal places. These are all such common types of data that Python already includes object types for these.Attributes are interesting because they’re kind of like a cubby storage system. Each attribute is just the name of a space in the cubby storage. And that storage is used to store objects that contain our data. In the cat example we have four attributes. Each of these attribute names is just the name of a cubby spot. We can use that storage to hold onto an object that contains some required data.
We’ve just reviewed the core concepts of Python's data model. Inside the Python runtime, basically, everything is an object. And objects can be formed together to model more complex concepts. Our cat for example is created using some of the built-in types.
Objects are kind of like Lego bricks in that they can be combined to model a concept. And then, that model itself can be used as it is, or it could become a building block for something else. In our cat example, we used built-in types to store attributes about cats. This cat object can function on its own. It has methods for playing, and meowing. Though, it could also be just another attribute on a higher level model. Imagine we want to model a cat cafe. In that case, we might have a list of Cats and methods for opening and closing the cafe. In this example, our Cat Cafe has a cats attribute, which is a list type that holds Cats. And each cat object includes its own attributes.
Understanding objects in Python is the foundation for mastering Python. Don't be worried if all of this is just a bit too abstract, or if it doesn't fully make sense to you yet. We'll keep building your understanding as we progress through the course.
For now, the key takeaways should be that:
- Objects are building blocks for modeling real world or abstract concepts.
- Python objects are a collection of attributes.
- Python attributes have two flavors, data and methods.
- Objects are foundational to Python because everything in Python is an object.
- Objects are like Lego bricks that can be combined to model more complex concepts.
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.