command pattern if __name__ ="__main__"acc accumulator( invoker calculationinvoker(invoker add_new_command(addcommand(acc )invoker add_new_command(subtractcommand(acc )invoker add_new_command(multiplycommand(acc )invoker add_new_command(dividecommand(acc )invoker run(print(accinvoker undo(invoker undo(invoker undo(invoker undo(print(accjust note that the __str()__ method is used when python casts the accumulator object to string when printing it as we have seen couple of times nowthe duck typing in python allows us to skip defining an overarching base class to inherit from as long as our commands have execute(and undo(methods and take receiver and value as parameters on constructionwe can use any command we feel like in the preceding codewe push every command that gets executed by the invoker onto stack thenwhen we want to undo commandthe last command is popped from the stack and the undo(method of that command is executed in the case of the calculatorthe inverse calculation is executed to return the value of the accumulator back to what it would have been before the command was executed all implementations of the command class in the command pattern will have method such as execute(that is called by the invoker to execute the command the instance of the command class keeps reference to the receiverthe name of the method to be executedand the values to be used as parameters in the execution the receiver is the only object that knows how to execute the method kept in the commandand it manages its own internal state independent of the other objects in the system the client has no knowledge of the implementation details of the commandand neither do the