Object-oriented programming organizes software around objects that preserve data and provide related functionality. In Python, objects are central to everyday programming, especially as instances of custom classes. section 10.0 section 2.4.1
Use the object-oriented style by:
- Determining the appropriate class for the task. section 2.4.1
- Creating an instance object from that class. section 2.4.1
- Accessing attributes with dot notation and calling methods with parentheses. section 2.4.1
Compile and reuse a regular expression
import re regex = re.compile("do") regex.pattern regex.search("do homework") regex.findall("don't do that") Here, re.compile creates a Pattern object, whose attributes and methods perform later operations. Reusing the compiled pattern can avoid compiling it again; direct re.search and re.findall calls provide a functional alternative. section 2.4.1
Figure 2.4 Applying the general OOP in pattern matching. In a general OOP approach, we first determine the proper class for the task. In this case, we use the Pattern class in the re module. The second step is creating the instance object. In the OOP paradigm, an object consists of attributes, which are accessible via dot notations, and methods, which are callable via parentheses. The third step is using the created Pattern object, such as by accessing its attributes or calling the methods.
Trace the three-step pattern: choose a class, create an instance, and use its attributes or methods. section 2.4.1








