Easy understanding of the policy mode of the python design mode, python Design Mode
This article provides examples of the python policy mode code for your reference. The specific content is as follows:
"Policy mode" import typesclass StrategyExample: def _ init _ (self, func = None): self. name = 'policy example 0' if func is not None: "is used to bind methods to the instance, and does not affect other instances" self.exe cute = types. methodType (func, self) def execute (self): print (self. name) def execute_replacement1 (self): print (self. name + 'slave execution 1') def execute_replacement2 (self): print (self. name + 'slave execution 2') if _ name _ = '_ main _': strat0 = StrategyExample () strat1 = StrategyExample (execute_replacement1) strat1.name = 'policy Example 1 'strat2 = StrategyExample (execute_replacement2) strat2.name = 'Policy Example 2 'strat0.execute () strat1.execute () strat2.execute ()
Running result:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.