Introduction
In this chapter, we will explore classes and class objects in Python. A class object, also called class instance, is a collection of attributes and values. For example, the following is a tabular view of a person object:
| Attribute | Value |
|---|---|
| Given Name | "John" |
| Family Name | "Adams" |
| Age | 54 |
| Sex | "Male" |
| Height | 180 |
| Hair Color | "Brown" |
This table describes various features of one person.
As we can see, a class object is very similar to a dictionary, which is a collection of keys and values.
A class object is an expression, so we can assign class objects to variables. For example, suppose that the variable profile contains some information about a person. To retrieve the person's age, we can write:
profile.age
To retrieve an attribute, we write a period (.) in between the variable name and the attribute.
If profile was a dictionary, we would write:
profile["age"]
The syntax is very similar with one key difference: while a dictionary key can be any (hashable) expression, object attributes must follow the same naming rules as variables. For example, the following is an invalid expression:
profile."first_name"
To update "age" to 27, we can write:
profile.age = 27
If profile was a dictionary, we would write:
profile["age"] = 27
Another similarity between class objects and dictionaries is that they are both stored in the Object Store. Thus, when we ask Python to assign a class object to a variable, Python assigns a Python reference that points to the object in the Object Store.
So far, class objects appear to be just dictionaries with a slightly different syntax. Let's now take a look at some features that make class objects standout from dictionaries.
Methods
The biggest difference between dictionaries and class objects is that we can define and call class methods.
Methods are functions that must be called with objects. Here is an example:
robot.walk(5)
robot is the variable that points to a class object, and walk is the method. 5 is a method argument.
We again use the period (.) in between the variable name and the method call. Note that the method call portion ( walk(5) ) looks exactly like a function call. Method call evaluation is also quite similar to the function call evaluation: Python jumps to the definition of the walk method and executes the method body.
This means that, like functions, methods need to be defined somewhere. But unlike a function definition, a method definition must be "inside" a class definition. As a reminder, a function definition looks like this:
def walk(robot, delta):
robot['distance'] = robot['distance'] + delta
A method definition looks like this:
class Robot:
def walk(self, delta):
self.distance = self.distance + delta
The first line is the class declaration. The second and third lines are the method definition. Method definitions are similar to function definitions except:
- All lines are indented by 1 more tab.
- The first method parameter is always the method call object (robot in our example).
We can have many method definitions inside a class definition. Here is an example:
class Robot:
def walk(self, delta):
self.distance = self.distance + delta
def turn(self, angle):
self.angle = self.angle + angle
def multiply(self, a, b):
return a * b
As we can see, classes give us the ability to group related functions.
Note that dictionaries also have methods (such as keys()). However, all dictionary methods are built-in; we cannot define custom dictionary methods.
Check all statements that are true.
Write a Python expression that sets the attribute weight of object person to 150.
Write a Python assignment where the target is number and the value is the attribute height of object person.
Comments
Please log in to add comments