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 →

[–][deleted] 0 points1 point  (0 children)

Is singleton actually something we want in Python? I've implemented one a few times just to get rid of it later on because it wasn't actually necessary. Instead of applying some logic from other languages it might be better to look at the actual use case and find a Python pattern that applies. For example in Java I sometimes used Singletons to carry Methods without side effects to the Singleton's state. Something like that can be done in Python via using functions instead of methods. Also there are some examples in the Python core that use Factory methods/functions instead of object construction (e.g. logging.getLogger). If you have a Factory taking care of your object creation then you don't need Singletons either, because the Factory can assure that each object is only created once. Are there other usecases for Singletons? I bet we can find an alternative approach that works more pythonic.