liveBook · Manning

Execute

As explained in Practices of the Python Pro

execute is a book-specific command method that performs the operation represented by a Command object.

execute is the shared command method that performs the operation represented by a Command object. A presentation layer can trigger different commands through this stable interface without knowing their implementations. section 7.2.2

A reliable execute interface should:

  • Keep the method available on every command class. section 7.2.2
  • Use a consistent signature so commands remain substitutable. section 8.4.1
  • Accept data explicitly when commands need to act on it. section 8.4.1
Listing 8.5. An abstract base class for the command pattern
from abc import ABC, abstractmethod          #1


class Command(ABC):                          #2
    @abstractmethod
    def execute(self, data):                 #3


class CreateBookmarksTableCommand(Command):  #4
    def execute(self, data=None):            #5
        ...


class AddBookmarkCommand(Command):           #6
    ...

Shows a Command abstract base class and concrete commands sharing an execute(data) contract. section 8.4.1

How Practices of the Python Pro covers it

How deep it goes
Defines an interface method, then reinforces it with an abstract base class and normalized method signatures.
Examples
Uses Bark commands, option selection, and bookmark commands to illustrate execution through a common method.
What it assumes
Assumes classes and interfaces, while developing Python-specific understanding of duck typing and abstract base classes.
What you can do after
Build it from scratch by defining a consistent execute method on commands and enforcing it with an abstract base class.
How it connects
Links command pattern, interfaces, abstract base classes, duck typing, and substitutability.

Related concepts

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