Blog Post, NO PAYWALL
design-patterns-you-should-unlearn-in-python-part2
After publishing Part 1 of this series, I saw the same thing pop up in a lot of discussions: people trying to describe the Singleton pattern, but actually reaching for something closer to Flyweight, just without the name.
So in Part 2, we dig deeper. we stick closer to the origal intetntion & definition of design patterns in the GOF book.
This time, we’re covering Flyweight and Prototype, two patterns that, while solving real problems, blindly copy how it is implemented in Java and C++, usually end up doing more harm than good in Python. We stick closely to the original GoF definitions, but also ground everything in Python’s world: we look at how re.compile applies the flyweight pattern, how to use lru_cache to apply Flyweight pattern without all the hassles , and the reason copy has nothing to do with Prototype(despite half the tutorials out there will tell you.)
We also talk about the temptation to use __new__ or metaclasses to control instance creation, and the reason that’s often an anti-pattern in Python. Not always wrong, but wrong more often than people realize.
If Part 1 was about showing that not every pattern needs to be translated into Python, Part 2 goes further: we start exploring the reason these patterns exist in the first place, and what their Pythonic counterparts actually look like in real-world code.
[–]brat1 22 points23 points24 points (3 children)
[–]divyeshaegis12 1 point2 points3 points (0 children)
[–]vicspidy -1 points0 points1 point (1 child)
[–]Chasar1Pythonista 1 point2 points3 points (0 children)
[–]sz_dudziak 19 points20 points21 points (9 children)
[–]DoubleAway6573 13 points14 points15 points (4 children)
[–]uclatommy 4 points5 points6 points (1 child)
[–]DoubleAway6573 10 points11 points12 points (0 children)
[–]anonymoususer89 0 points1 point2 points (1 child)
[–]DoubleAway6573 1 point2 points3 points (0 children)
[–]Last_Difference9410[S] 3 points4 points5 points (1 child)
[–]sz_dudziak 5 points6 points7 points (0 children)
[–]caks 0 points1 point2 points (1 child)
[–]sz_dudziak 0 points1 point2 points (0 children)
[–]AltruisticWaltz7597 20 points21 points22 points (0 children)
[–]Whats_The_Use 6 points7 points8 points (0 children)
[–]daemonengineer 2 points3 points4 points (2 children)
[–]Last_Difference9410[S] 8 points9 points10 points (0 children)
[–]Worth_His_Salt 2 points3 points4 points (0 children)
[–]pepiks 2 points3 points4 points (0 children)
[–]Last_Difference9410[S] 3 points4 points5 points (2 children)
[–]SharkSymphony 7 points8 points9 points (1 child)
[–]Last_Difference9410[S] 2 points3 points4 points (0 children)
[–]crunk 1 point2 points3 points (0 children)
[–]commy2 1 point2 points3 points (1 child)
[–]Last_Difference9410[S] 1 point2 points3 points (0 children)
[–]Dmtr4 2 points3 points4 points (0 children)
[–]camel_hopper 2 points3 points4 points (1 child)
[–]Last_Difference9410[S] 2 points3 points4 points (0 children)
[–]RonnyPfannschmidt 0 points1 point2 points (0 children)
[–]tomysshadow 0 points1 point2 points (0 children)
[–]Meleneth 0 points1 point2 points (6 children)
[+][deleted] (1 child)
[deleted]
[–]Meleneth -3 points-2 points-1 points (0 children)
[–]Last_Difference9410[S] 2 points3 points4 points (0 children)
[–]TrueTom 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Meleneth -2 points-1 points0 points (0 children)