From Model to Code
Start course

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.

Learning Objectives

Upon completing this course you’ll be familiar with the anatomy and behaviors of objects.

Intended Audience

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.


We’ve spent a lot of time up till now focused on understanding different aspects of objects. Objects are Python’s low-level building block. Which means understanding objects is the path towards mastering Python. There’s still plenty to learn about objects. But, we’ve reached a bit of a turning point. 

We’ve covered just enough information regarding objects that we’re ready to see how an object relates to Python code. I want to introduce you to some Python code so that you can start mentally mapping the concept of an object to Python’s code. Thus far we’ve been visualizing objects in the abstract. As we go forward, the goal will be to get you thinking about objects in terms of Python’s syntax. 

That term, syntax relates to the structure of the Python code. Written languages have rules about the order of words, and the structure of a sentence. Programming languages are similar and have a set of rules that must be followed. We refer to all of those rules as a language’s syntax.

So, when I say that you’ll be thinking about objects in terms of Python’s syntax, what I’m saying is that: Once you understand the concepts you’re trying to model, you’ll be able to picture how to model it with Python code.

Let’s recap some details about objects before we get started.

We know that:

  • 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.

So, we use objects to model a concept and its behaviors. In a previous lesson, we modeled a numeric Counter. The purpose of the counter is to increment a number by one every time an event occurs.

We kept the counter simple with one attribute to store the number we’re going to increment, and a method that when called will perform the increment operation. 

In this lesson, we’re going to review the Python code required to create our counter type. We’re not going to dive deep into the code. Rather we’ll focus on the intent of the code. So don’t worry about the exact syntax in this lesson.

Let’s start by comparing our conceptual counter to the Python code required to create a counter object. Here’s what we know about our model. Our model is named counter. It includes one attribute named number that is set to zero by default, and one method named count that increments the counter by one. 

That’s three things that we know about our model. 

  1. The name is counter
  2. There’s an attribute named number
  3. There’s a method named count

Focusing just on those three things, we can see that these are reflected in our code. At the top we can see the name Counter. We can see the attribute named number mentioned twice. And we can see the method named count. 

I’m going to explain what this code does at a high level and then we’ll go through some of the finer points. 

This code defines a new object type named Counter. It defines an attribute named number that is set to zero when the object is created. And it defines a method named count that increments the number attribute when the method is called.

I want to clarify that this code doesn’t actually create a counter object. Recall that an object’s type is the name of the concept that we’re modeling. This code only creates a new counter type. We can use this type to create actual objects.

Notice the first line which reads: class Counter followed by a colon. This is how Python defines new object types. A class is just a blueprint for creating a specific type of object. In this case our counter class creates objects of type: Counter.

The next line defines a method with this strange name double-underscore init double-underscore. We’re not going to go in depth on this. However, this is a special method called a constructor that allows us to specify the code to run when an object is created.

This is a topic we’ll cover in a future lesson. For now, all you need to know is that this constructor method is going to run whenever a counter object is created.

This code creates the attribute named number and sets its value to zero. So, this section is responsible for setting up the number attribute. 

This next section defines the count method. This is the method responsible for adding one to the number whenever the method is called. It consists of just the one line which you see references the number attribute. 

So, this code creates a new Counter object type. Let’s see the code used to actually create a counter object and call the count method.

​​I don’t want you to try and focus on the syntax. Rather let’s review the intent of each line.

This first line creates a new counter object.  Then it displays the counter’s number. Which is zero.

Once we call the count method, the number will change to 1. This line here calls the count method. And we can see that the number is now 1.

If we call the count method 4 more times, we can see that the number is 5.

So, this shows that both our concept of a numeric counter and the code used to model the concept are aligned. We can call the count method and increment the number by one each time.

We’ll be reviewing Python’s syntax in future lessons. So don’t don’t worry if this was too high-level of an introduction.

This was just to help bridge the conceptual idea of an object with Python’s syntax.

Okay, this seems like a natural stopping point. Your key takeaways for this lesson should be that:

  • In Python class definitions are used to create new object types.
  • Class definitions allow us to specify the code to run when an object is created in the constructor method.

Alright, that’s going to wrap up this lesson. Thanks so much for watching and I’ll see you in another lesson!

About the Author
Learning Paths

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.

Covered Topics