TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python-patterns – A collection of design patterns/idioms in Python

262 pointsby giladalmost 3 years ago

13 comments

tonnydouradoalmost 3 years ago
Mostly, it&#x27;s Java written with Python syntax, specially the gang-of-four patterns. I recall it being even more java-like, so, progress?<p>The stuff that is actually idiomatic python is either too convoluted or rather obvious.<p>I know this comes across rather mean, but this is just not a good resource, for either beginners (not very idiomatic) or experienced developers (trivial patterns with trivial examples). There&#x27;s maybe 2 or 3 good ideas in there, but even those I might be hard pressed to find a real usecase for.
评论 #31568693 未加载
评论 #31570947 未加载
评论 #31576519 未加载
评论 #31568786 未加载
asicspalmost 3 years ago
See also:<p>* &quot;Python Design Patterns&quot;: <a href="https:&#x2F;&#x2F;python-patterns.guide&#x2F;" rel="nofollow">https:&#x2F;&#x2F;python-patterns.guide&#x2F;</a><p>* &quot;Clean Architectures in Python&quot;: <a href="https:&#x2F;&#x2F;www.thedigitalcatbooks.com&#x2F;pycabook-introduction" rel="nofollow">https:&#x2F;&#x2F;www.thedigitalcatbooks.com&#x2F;pycabook-introduction</a><p>* &quot;Architecture Patterns with Python&quot;: <a href="https:&#x2F;&#x2F;www.cosmicpython.com&#x2F;book&#x2F;preface.html" rel="nofollow">https:&#x2F;&#x2F;www.cosmicpython.com&#x2F;book&#x2F;preface.html</a>
mumblemumblealmost 3 years ago
For an alternative perspective, allow me to present some classic talks from Raymond Hettinger.<p>First, some generic tips on how to write Python code that&#x27;s pleasant to work with: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;OSGv2VnC0go" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;OSGv2VnC0go</a><p>And then this one digs more specifically into how not to write Python like it&#x27;s Java, starting at 12:40 (with talking about why leading up to that): <a href="https:&#x2F;&#x2F;youtu.be&#x2F;wf-BqAjZb8M" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;wf-BqAjZb8M</a>
axblountalmost 3 years ago
I&#x27;m confused by the Borg pattern. It seems like a singleton that tricks the user into believing there are multiple instances (very bad imo). Is there any advantage over singleton? Maybe some kind of `__dict__` behavior I&#x27;m not aware of?
评论 #31570928 未加载
lordgrenvillealmost 3 years ago
I&#x27;ve had Python code reviewed in the past by senior devs from more of a Java&#x2F;C# background, who encouraged me to read the GoF and rewrite classes using the abc (abstract base class) library, staticmethod&#x2F;classmethod decorators, etc. I never quite understood the value of it in Python. But the fact that those are included in the stdlib makes me think that they must have some clear use cases.
评论 #31573462 未加载
havan_agrawalalmost 3 years ago
Picking very specifically on the builder pattern: the whole point is to solve one or more of the following problems:<p>1. creating complex objects and having them valid at creation<p>2. many optional arguments for an object&#x27;s construction<p>3. similar types for object construction<p>For e.g. in Java you might see something like:<p><pre><code> Point p = new Point(10, 0, 5); &#x2F;&#x2F; (x=10, z=5) Point q = new Point(10, 2); &#x2F;&#x2F; (x=10, y=2) </code></pre> which may be problematic because<p>1. y needs to be specified explicitly to 0, because you can&#x27;t expose an overloaded constructor that takes x and z (that would clash with the constructor that takes x and y<p>2. It _may_ not be evident that new Point(10, 0, 5); passes x, y and z in that order<p>3. It ties the constructor to the implementation. You can never expose a Point(double radius, double theta) constructor because that would clash with the Point(double x, double y) constructor.<p>The builder would solve all of these problems:<p><pre><code> Point p = Point.newBuilder() .setX(10) .setZ(5) .build(); </code></pre> Python however doesn&#x27;t necessarily need this pattern because idiomatically solves all three of these problems by providing<p>1. named arguments, and<p>2. default arguments<p>so you could have:<p><pre><code> p = Point(x=10, z=5) q = Point(10, 2) t = Point(radius=5, theta=45) </code></pre> I feel the post completely glosses over this by providing a single super-class with the appropriate constructor, and overriding the methods in the sub-classes. What if I just have a single class that I want a builder for? I don&#x27;t think it showcases the builder pattern at all, just method overloading in classes.
jjicealmost 3 years ago
I recently began reading GOF (a bit into the behavioral patterns at the moment). A lot of those patterns were ideas I had been familiar with, but not through a formal specification, likely due to seeing so much code inspired by the book. I just find it so fascinating that these patterns were written about in 1994 (?) and they were pretty well designed.<p>I will say, the book is heavily focused on OOP, which has fallen out of favor a bit with newer languages (but obviously very alive), and it&#x27;s almost like a time piece. Feels like OOP was seen as this answer to so many business problems and organization of code.<p>Just interesting to imagine OOP at a time when it was new and exciting, since I grew up at a time when Java was ubiquitous and everyone was at least a little OO.
TekMolalmost 3 years ago
I wish there was a pattern to encapsulate data and functions from a module.<p>So you could do:<p><pre><code> import greeter greet1 = encapsulate(greeter) greet2 = encapsulate(greeter) greet1.name = &#x27;Joe&#x27; greet2.name = &#x27;Sue&#x27; greet1.greet() greet2.greet() </code></pre> And greeter.py would look like this:<p><pre><code> name = &#x27;nobody&#x27; def greet(): print (&#x27;Hello &#x27;+name) </code></pre> Output:<p><pre><code> Hello Joe Hello Sue </code></pre> This would be so much nicer and leaner than classes.
评论 #31567727 未加载
评论 #31569086 未加载
评论 #31567615 未加载
评论 #31567192 未加载
评论 #31566941 未加载
评论 #31570900 未加载
评论 #31566633 未加载
评论 #31566771 未加载
scaraffealmost 3 years ago
Are there real world exercises where I could apply these patterns?
mariorojasalmost 3 years ago
I&#x27;m beginner in Python&#x2F;Django and to be honest I feel confused since those patterns add complexity to the code.
adammarplesalmost 3 years ago
please, don&#x27;t do any of this in your python, thanks
评论 #31576469 未加载
评论 #31571991 未加载
评论 #31570432 未加载
Kyragemalmost 3 years ago
GOF is quite inaccessible&#x2F;boring to students, but the headfirst design patterns book has been a blast in my design patterns course.
visargaalmost 3 years ago
This makes Python have Perl vibes.
评论 #31566708 未加载