From the course: Python: Advanced Design Patterns (2018)
Unlock the full course today
Join today to access over 24,300 courses taught by industry experts.
Interpreter example - Python Tutorial
From the course: Python: Advanced Design Patterns (2018)
Interpreter example
- [Instructor] Pattern three has a built in feature that allows us to designate a certain method as an abstract method. When a method is decorated as an abstract method it should be overridden by a subclass. To use this handy Python feature, let's import abstract base classes, or in short, abc. Type from abc import ABC, abstractmethod The interpreter pattern needs an abstract expression class which is our abstract class that should contain an abstract method called interpret. We decorate this method with at abstract method to force it to be overridden. So type @abstractmethod def interpret(self): pass Now let's define our non-terminal expression class which inherits from the abstract expression class. So here is the nonterminal expression class, inheriting from abstract expression class. The constructor here stores an expression when a non-terminal expression object is created. So type self._expression = expression The class also implements the interpret method. So the interpret…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.