Design Patterns – Command in Python

Jigisha Sata

a year ago

Python Design Patterns
Python Design Patterns
It adds a level of abstraction between actions and includes an object, which invokes these actions.
In command design pattern, the client creates a command object that includes a list of commands to be executed.
The basic architecture of the command, the pattern is shown in the figure below.
Basic Architecture of the command pattern
Basic Architecture of the command pattern
Implementation of the command pattern?
Now let’s see how to implement the design pattern.
def demo(a,b,c):

class Command:
   def __init__(self, cmd, *args):

   def __call__(self, *args):
      return apply(self._cmd, self._args+args)
cmd = Command(dir,__builtins__)
print (cmd())

cmd = Command(demo,1,2)

The output is shown below:
Output for design pattern
Output for design pattern
The above code implements all the commands and keywords listed in Python language. It also prints all the necessary values of the variables as per the requirement.
To know more about Design Patterns - command in python join our courses.
I hope you enjoyed reading this article and finally, you came to know about Design Patterns – Command in Python
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review

We're Online!

Chat now for any query