The strategy pattern: write BETTER PYTHON CODE Part 3
Video Statistics and Information
Channel: ArjanCodes
Views: 25,585
Rating: 4.9870968 out of 5
Keywords: write better python code, strategy design pattern tutorial, design patterns through python, design pattern in python tutorial, strategy design pattern python, strategy design pattern python example, strategy pattern python tutorial, strategy design pattern, design pattern video tutorial, design patterns, software architecture, visual studio, design patterns tutorial, software architecture patterns, strategy pattern python, Software architecture interview questions
Id: WQ8bNdxREHU
Channel Id: undefined
Length: 15min 30sec (930 seconds)
Published: Fri Feb 05 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
https://github.com/egges/betterpython/blob/main/3%20-%20strategy%20pattern/strategy-after.py#L68
Just a heads up, the standard pythonic way to express this is:
Thanks, it was very useful for me.
Great video, thank you!
I have some feedback:
instead of typing the implementation of the strategies, I think it would have been nice to retractor the code from the if-else block into those methods in the strategy classes. I say this because people usually realize they could have used the strategy pattern after the fact, and refactoring would emphasize that it's the same code that's being executed.
one of the disadvantages of the functional approach is that you can't pass other parameters into the strategy functions unless you have nested methods (like in java or c#). Not sure if it's possible with Python as it's not my main language. The up side of strategy classes is that you can pass in other information in the constructor, which sets up the factory pattern for one of your videos if you haven't covered it already :)
I enjoyed your video, there are two suggestions I would make regarding the code:
In python you should never name a variable the same as a reserved word such as list. This actually overwrites the built in list function. So for example the following code will produce the following results.
Additionally, the copy methods you are using only return "shallow" copies of the input list. Which means that any changes you make to the copy also happen to the original list.
More info regarding copy can be found here in the python documentation: https://docs.python.org/3/library/copy.html
Keep up the good work!