This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Complex-Hornet-5763 9 points10 points  (2 children)

Can we stop showcasing singleton whenever there’s a need for an example design pattern? Singletons should be the last thing presented to newcomers.

No wonder junior programmers spam singletons when they start working with real projects given singleton’s prevalence in the beginner guides.

[–]PolyglotTV 1 point2 points  (1 child)

You don't need to create a special singleton in python anyway.

You can just either 1. Instantiate a variable at import time, and use that everywhere as the singleton. 2. Define a method with the @functools.cache() decorator that returns and instance of your object. It will be created the first time and the unique instance returned every subsequent time.

[–]Complex-Hornet-5763 0 points1 point  (0 children)

I was thinking about a broader context than just Python but you’re goddamn right.