liveBook · Manning

Object-oriented programming

As explained in Python How-To

Object-oriented programming is a programming paradigm that organizes software around objects containing data and related behavior.

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.
CH02_F04_Cui

Trace the three-step pattern: choose a class, create an instance, and use its attributes or methods. section 2.4.1

How Python How-To covers it

How deep it goes
Defines classes, instances, attributes, and methods, then demonstrates object use without exhaustively covering Python’s object model.
Examples
Uses compiled regular expressions, Pattern methods, and direct re-module function calls to contrast object-oriented and functional styles.
What it assumes
Assumes basic Python use while establishing object fundamentals through custom classes, variables, callability, and object inspection.
What you can do after
Apply it via Python’s re module by compiling a Pattern object and invoking its attributes and search methods.
How it connects
Connects custom classes, instances, attributes, methods, regular expressions, Pattern objects, and functional programming.

Also explained in

Compare how 9 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.