Read Attributes and Method Calls
Read dot notation from the object outward, distinguish selecting an attribute from calling a method, and keep dictionary keys separate.
The Chapter 8 report used expressions such as path.name and
counts.most_common(3). Both place a name after a dot, but they do different
work. Reading library code becomes easier once we separate selecting an
attribute from calling a method.
Read the Object Before the Dot
Begin with the path to the measurement file:
measurements.txt
An object is a Python value. Here, path refers to a Path object. An
attribute is a named value provided by an object. Read path.name from
left to right:
- take the object referred to by
path; - select its attribute named
name; - use the value stored there.
No parentheses follow name, so this expression selects a value without
calling it. The attribute contains the final name in the path.
Q1. Read a path attribute
Given path = Path("reports/summary.txt"), which expression obtains the final
name "summary.txt"?
Select one choice, then check.
HintLook for a value rather than an action
The documentation describes name as an attribute containing the final path
component.
SolutionSelect path.name
Use path.name. The expression selects the name attribute and returns the
string "summary.txt".
Selection and Calling Are Separate Steps
An attribute can itself be a method. A method is a function selected from an object. Consider a short piece of text from the measurement data:
<class 'builtin_function_or_method'>
['north', 'west', 'café']
text.split selects the method object. It does not split anything.
text.split() selects that method and then calls it. The parentheses are the
visible difference between keeping an operation and performing it.
Arguments go inside the call parentheses. Chapter 8 counted the accepted
sensor labels with a Counter:
[('north', 2), ('west', 2), ('café', 1)]
Read counts.most_common(3) in four parts:
| Part | Role |
|---|---|
counts | the Counter object |
.most_common | select the method |
(3) | call it with the argument 3 |
| returned value | a list containing three (label, count) tuples |
The result is a new list. It is not an attribute stored permanently under the
name most_common.
Q2. Separate selection from a call
What value does the final expression produce?
Select one choice, then check.
HintFollow the parentheses
method refers to the same callable selected by text.split. The final
parentheses perform the split.
SolutionThe call returns three words
method() calls the selected split method. With no separator argument, it
returns ["north", "west", "café"].
A Key Is Not an Attribute
The parser represented one accepted row as a dictionary in this example:
Square brackets ask the dictionary for the value stored under the key
"label". A dot asks an object for an attribute. These requests are not
interchangeable:
print(record.label)
AttributeError: 'dict' object has no attribute 'label'
The error is evidence. It names the actual type, dict, and the missing
attribute, label. Inspect the object before trying another spelling:
<class 'dict'>
dict_keys(['label', 'reading'])
The type and keys show that record["label"] is the intended access. If the
object were a class instance with a label attribute, dot notation could be
correct. The object's type determines which names and operations it provides.
Q3. Repair three dot expressions
Repair the marked expressions. Read the path's final name, read the dictionary
key, and call most_common so the program prints the three expected lines.
Editable Python
Ready to run.
HintMatch syntax to the object
A Path supplies name as an attribute. A dictionary stores the two values
under string keys. A method performs work only when called.
SolutionUse an attribute, two keys, and one call
Each expression now matches the kind of access supported by its object.
Read dot notation from the object outward. A dot selects an attribute; parentheses call a selected method; square brackets perform a dictionary-key lookup. When an attribute is missing, use the reported type as evidence before guessing. Documentation can then tell us what arguments a valid call accepts and what value it returns.