13.1. What are Objects¶
A quick search of the web returns this fact:
Python is an Object-Oriented Programming language.
Object-oriented programming (OOP) is a software design model that focuses on the capabilities of object types instead of on things like individual functions, loops, logic, etc. In this book, we won’t get into the principles behind OOP. However, we do need to take a closer look at the concept of objects.
We have worked with objects many times already: turtle objects, string objects, list objects, etc. However, we never explained what objects represent in Python.
As a first step, let’s review some vocabulary:
A value is a specific piece of data, like the integer
3, the string
'hello', or the list
[33, 45.2, 8].
A function is a defined, reusable block of code that performs a single, small task.
We assign values to variables so we can use the data later in our program. We define and call functions to do something with data or perform actions on it. When we set up our programs this way, data storage and data manipulation get carried out separately.
Objects do both. They store data and perform actions on that data. Objects give us a way to bring together all the parts of our code that relate to each other.
The data assigned to an object is called its properties. The actions an object can perform are called its methods.
Think of an object like a smart phone.
The properties of an object give us information about it. For a phone, this data includes things like brand, size, mass, color, amount of memory, etc.
The methods of an object determine what it can do. For a phone, this would be the apps it contains. These allow the object to do things like take photos, send text messages, pay a bill, or even make a voice call.
13.1.1. Examples of Objects¶
Let’s begin with a familiar example: strings.
text = "I am an object."
All strings have a length property. This tells us the number of characters between the quote marks. Each individual character can be classified as a letter, digit, punctuation, etc. Strings also include a property that describes their case (upper or lower).
In addition to these properties, we can perform a defined set of actions on ANY
string value. These methods take the original
string as data and return a new string. Examples include
Since strings contain both properties and methods (data and behaviors), they are objects.
Another example of a Python object is a turtle.
bob = turtle.Turtle()
Properties for the
bob object include its color, speed, pen size, shape,
and location on the screen. Each of these bits of data tells us something about
the object. Methods include actions like
Anything related to the appearance or behavior of a turtle on the screen is
included with every turtle object. We do not need to define a
function to rotate a turtle. The code for that behavior is part of the object
13.1.3. Check Your Understanding¶
Assume we create an object of type
rabbit in our code. Which of the
following are properties for this kind of object, and which ones are
methods? (Answer in your head before clicking each option).