from functools import lru_cache
class Whatever:
pass
@lru_cache(maxsize=1)
def get_whatever():
return Whatever()
And use `get_whatever` as your interface to get the resource.Design patterns you should unlearn in Python
11–20 of 110 posts
Re: Design patterns you should unlearn in Python
#12Re: Design patterns you should unlearn in Python
#13The Zen of Python: there should be one obvious way to do things. Python in practice: there is more ways of doing it than in any other programming language. Oh Python, how I love and hate you.
Re: Design patterns you should unlearn in Python
#14Re: Design patterns you should unlearn in Python
#15Conflating key/value lookups (dicts) with structured data (classes). They are both useful tools, but are for different purposes. Many python programmers (Myself many years ago included!) misused dicts when they should have been using dataclasses.
Re: Design patterns you should unlearn in Python
#16Re: Design patterns you should unlearn in Python
#17I like the concept of the article but I’m not sure I’ve seen these in the wild.
In my opinion the singleton pattern does however not make Python code harder to test. In Python it's actually extremely handy, because its incredibly easy to mock out a singleton in your test cases.
Re: Design patterns you should unlearn in Python
#18Peter Norvig has a well-known piece that goes into more depth on why the GoF-style patterns don't make much sense in high-level languages: https://www.norvig.com/design-patterns/
Re: Design patterns you should unlearn in Python
#19First thing I looked up in the article was "singleton", as a sanity check, whether the article is any good. And yes, it shows module level binding as alternative, exactly what I expected, because I looked into this in the past, when someone implemented API client singleton, in a case of irrelevant early optimization of something that was never a bottleneck.
Articles like this are helpful in spreading the awareness, that one should not hold a Python like one holds a Java.
Re: Design patterns you should unlearn in Python
#20A good example from my experience might be connecting to a Cassandra cluster it other type of database that can have extremely complex distributed settings and behaviors: timeouts, consistency levels, failure modes, retry behavior, seed connector sets.
Javaland definitely had a problem with overuse of patterns, but the patterns are legitimate tools even outside of Oop.
I haven't done much research into testing frameworks in other languages, but the spock testing framework in groovy/javaland is a serious piece of good software engineering that needs singletons and other "non hard coded/not global" approaches to work well.
Spring gets a ton of hate outside of jabs, and I get it, they tried to subsume every aspect of programming and apis especially web into their framework, but the core spring framework solved complex object graph construction in a very effective way
Oh you hate "objects" but have thousand line struct graph construction code?
It's kind of sad that groovy never took off. It offered all the good parts of java with a ton of good python, ruby, and other langs with the solid foundation of the jvm for high speed execution.
But it's effectively dead. Kind of like Cassandra is effectively dead. The tech treadmill will eventually leave you behind.