13.5. Create a Class¶
13.5.1. What Classes Do¶
We often create many objects of the same type, like a series of whole numbers or several string values. Classes allow us to do this in an efficient way.
Think of classes like cookie cutters. They provide the structure for making multiple examples of the same data type, and they make sure each object can execute the same methods. We reuse a class every time we need to generate a new object.
Note
Although classes create new objects, the objects themselves will NOT be exactly the same. For example, all string values have a length, but the value for the length depends on the number of characters in each string.
Objects of the same type have the same property names and methods. However, the values of the properties can differ.
Think of this like using the same cookie cutter to make sugar cookies vs. gingerbread cookies. Each cookie will have ingredients and flavor properties, but these will have completely different values depending on the recipe.
Let’s revisit the dog
object we used earlier to see how it relates to a
class.
Each object contains the same property names, and each one can execute the same set of methods. The three objects all belong to the same class, even though their property values differ.
13.5.2. Design a New Class¶
Before we create a new class, we should begin by describing the objects that belong in that class. Take time to think about the properties and methods we want each object to have. Next, write these ideas down! Doing this will make designing and coding the class easier.
Try It!
Let’s say we want to create a set of cat objects. Take a moment to think about the properties the objects should have. We should be able to use these properties to describe each cat.
The values of the properties will allow us to tell the difference between animals, but for now just focus on coming up with a set of property names.
The editor below is NOT here to run code. Instead, use the space to add names for any properties you think belong with a cat object. Two examples have already been given.
Add at least two more property names to the editor, but feel free to go crazy!
If you are logged into your Trinket account, you can click the Remix button to save your ideas. We will use this editor again on a later page. Making your own copy will allow you to pick up where you left off. If you do not have a Trinket account, copy and paste your list of method names into something you can look at later.
Now let’s convert our thoughts into code.
Tip
In this design phase, we want to include as many property names as we can.
When we start coding, we should keep things simple and build something small that works. We won’t include all of our ideas right away.
Once we have working code, we can always update it later to make the class more powerful.
13.5.3. Define the New Class¶
To define a new class, begin with the class
keyword, followed by the name
of the class. The general syntax is:
1 2 | class ClassName:
# Class code...
|
Note that class definitions do NOT include parentheses, ()
.
Just like with variable and function names, Python coders follow a set of rules and recommendations when defining a new class.
Class names begin with a capital letter, followed by lowercase letters. For example,
Cat
.For class names that contain more than one word, begin each new word with a capital letter. Do not use underscores to separate the words. For example,
CoolCat
. (FYI: This style is called UpperCamelCase).If the class name contains an abbreviation, use capital letters for that abbreviation. For example,
OSUBuckeyes
.
13.5.3.1. Setting Property Values¶
Each new object contains a set of property values. To assign these values, our
class must include an initializer method. This method is defined like any
other function, but it always gets the special name __init__
(two
underscores, then the letters, then two more underscores).
For our Cat
class, this looks like:
1 2 3 | class Cat:
def __init__(self):
# Assignment statements...
|
The initializer method automatically runs whenever we call a class, and it assigns values to each property name.
The self
parameter requires some explanation, and we will use an example to
help set up that discussion.
Try It!
Use the instructions and editor below to complete the __init__
method:
To assign a value to a property, the syntax is:
self.property_name = property_value
On line 5,
self.name = 'Coach'
assigns the value'Coach'
to the property calledname
.On line 6, add the statement
self.age = 2
to assign the integer2
to the property calledage
. Be sure to indent the statement to match line 5. This putsself.age = 2
inside theinit
code block.Pick one of the properties you listed earlier on this page. On line 7, assign a value to that property.
Paste the following code on lines 9 and 10. To keep the statements outside of the class, do NOT indent them.
9 10
cat_1 = Cat() print(cat_1.name, cat_1.age)
Line 9 creates a new object called
cat_1
, and line 10 prints two of its properties. Run the program to check your work. Properly done, the output should be:Coach 2
Modify line 10 to print all three property values.
Finally, create another object called
cat_2
and print out its property values.
Lets step through the final code:
On line 9,
Cat()
calls the class to create a new object.Control moves to line 1, and the class statements execute.
The
__init__
method runs, and it assigns values to each of the properties included in the code block. This is whereself
plays a role.When we call a class,
self
takes on the name of the new object. Whencat_1 = Cat()
executes,self
gets assigned the valuecat_1
. So:self.name = 'Coach'
evaluates ascat_1.name = 'Coach'
self.age = 2
evaluates ascat_1.age = 2
etc.
After the
__init__
method finishes, control passes back to line 9, and the new object is assigned to the variablecat_1
.In this Try It example, the
__init__
method causes every newCat
object to start with aname
of'Coach'
, anage
of2
, and the third property you defined.
The program creates two cat objects, cat_1
and cat_2
, that have the
same property values. Are the two objects the same? To answer this question,
add the statement print(cat_1 == cat_2)
to the bottom of the code and run
the program again. Is the output True
or False
?
Tip
Think of cat_1
and cat_2
like twins. Even though they have identical
properties, they still represent separate animals. They might look and
behave exactly the same, but they are different objects!
13.5.4. Use Parameters with __init__
¶
Once we create cat_1
and cat_2
, we can easily change the values for
the name
and age
properties.
cat_1.name = "Garfield"
However, it would be better if we could assign these values when the objects
are first created. Instead of giving every new Cat
object the same name
and age, we want to let these values vary from object to object.
We do this by adding parameters to the __init__
method:
1 2 3 4 | class Cat:
def __init__(self, a_name, an_age):
self.name = a_name
self.age = an_age
|
Modify the code in the editor above to match this format. Leave the third property assignment alone for now.
Run the program again. The statement
cat_1 = Cat()
should now throw an error. By adding parameters to__init__
, Python expects values to be included when we call the class, but we did not provide any.Add arguments for a name and an age in the statement. For example,
cat_1 = Cat('Nala', 4)
. Do the same forcat_2
, but use different values.Run the program and fix any remaining bugs. Try changing the arguments you send to the class when creating a new object. How do those changes affect the output?
Next, follow a similar process for the third property. Define a parameter and include an argument when you call the class.
13.5.5. Check Your Understanding¶
The questions below refer to a class called Car
.
1 2 3 4 5 6 7 | class Car:
def __init__(make, model, year, color, mpg):
self.make = make
self.model = model
self.year = year
self.color = color
self.mpg = mpg
|
Question
If we call the class with my_car = Car('Chevy', 'Astro', 1985, 'gray', 20)
,
what is output by print(my_car.model)
?
- my_car
- Chevy
- Astro
- 1985
Question
If we create another object called other_car
with the exact same
property values, what is the result of the expressions
my_car == other_car
and my_car.year == other_car.year
?
- True/True
- False/True
- True/False
- False/False
Question
What happens if we call the class with my_car = Car('Tesla', 'Model S',
2020, 'blue')
?
- The mpg property is assigned a value of 0.
- The mpg property is assigned the empty string.
- The mpg property is not added to the my_car object.
- The program crashes and displays an error message.