I've never seen anybody do that... In Python you can use a module as a singleton (mentioned in the article). Or provide some data like:

  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.

You can implement a singleton class that works properly quite easily. The advantage is that most people are familiar with singleton as a pattern, and it is a self contained chunk of code. The cache solution you provided works, but its functionality is not obvious and it feels very hacky to me. Somebody's going to initialize Whatever in another way down the line without using the cached function...

Another technique I've seen is to hide or overwrite the name of the class, such that client code only knows about the instance (and is expected to use it directly rather than doing any kind of access or instantiation). Of course, this doesn't give you lazy initialization unless you lazily import the module, at which point you would definitely be better off just using the module object directly.

There's also code out there which replaces the cached module object with a class instance in top-level code! This is especially used to work around the prior lack of module-level `__getattr__` (and `__dir__`), added in 3.7 (https://peps.python.org/pep-0562/). But you might still need it if for some reason you want to hook into the lower-level `__getattribute__`. And Andrew Moffat's `sh` package still does this (https://github.com/amoffat/sh/blob/develop/sh.py#L3635) even though it now only declares support for 3.8 and above. (Perhaps there was simply no clear reason to change it.)

I do this in fast api and then pass get_whatever as a dependency to an endpoint

> I've never seen anybody do that...

I feel the blog post is a bunch of poorly thought through strawmen. I was browsing through the singleton example and I was wondering why would anyone use buggy code to implement something it clearly was not designed to implement.

The whole article is quite subpar. I was expecting idiomatic stuff that eliminated the need to implement something, like for example implementing singletons with modules and even getter functions, but there was none of that: just strawmen.

Really disappointing.