★ wanayoo — archive 1999 https://github.com/Technologicat/unpythonic/issues/22Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Borg class? #22

Closed
Technologicat opened this issue Aug 16, 2019 · 1 comment
Closed

Add a Borg class? #22

Technologicat opened this issue Aug 16, 2019 · 1 comment

Comments

@Technologicat
Copy link
Owner

@Technologicat Technologicat commented Aug 16, 2019

A cool hack from ActiveState recipes. Different instances, all sharing the same state.

OTOH, it's really simple and already documented in the ActiveState recipes, so maybe we don't need to have it here.

@Technologicat
Copy link
Owner Author

@Technologicat Technologicat commented Mar 4, 2020 •

Cool hack, but useless? Defeats using is for an identity check, yet the state is shared.

At least to me, one of the main use cases of a singleton in Python is a stateless, unique marker value, which can be identified with a quick object identity check (is). The other main use case is when there is a global service, which must be instantiated at most once per process. (But be very, very careful with that - is it for certain that there can't arise a situation in which multiple independent instances of the service are needed simultaneously?)

(Lispy symbols can be used as sentinel values, but a singleton may be better if the object needs to have some functionality beside just being a marker. For example, nil supports the iterator protocol.)

In Python, the constructor syntax issue for singletons is a herring, because __new__ can be customized, making singleton creation look just like regular object creation. (Whether or not that is a good idea is a different matter.)

Since modules are effectively singletons already, for any object type that is to be a singleton, in my opinion The Pythonic Thing is to make the constructor raise TypeError if it is called while an instance already exists. Separate the concerns: let the user code manage the object reference, just like for regular objects. This leaves just enforcing the at-most-one-instance-only guarantee as the sole concern of Singleton.

It is trivial to grab a reference from the top-level scope of another module, thus performing the job of the singleton instance accessor (as seen in static languages). Also, guaranteeing that the instance has been created before any other module tries to access it is trivial (modulo any circular imports).

We now have such a pythonified, thread-safe singleton abstraction as unpythonic.Singleton. It interacts properly with pickle, too - unpickling returns the existing instance (if any) and, by default, overwrites its state.

We now also have lispy, interned, named symbols with a similar level of pickle support and thread-safe instantiation, as unpythonic.sym. See also unpythonic.gensym to generate guarantee-unique uninterned symbols (also with pickle support). Symbols are a relative of the singleton, since there is only one object instance for each unique symbol name (or UUID in the case of gensyms).

Since I can't think of a use case that Borg (a.k.a. Monostate) serves better than Singleton (in Python!), I'm closing this as a bad idea - at least for now.

If a reason to use a Borg rather than Singleton comes up later, this can be revisited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.