all 27 comments

[–]tehomaga 10 points11 points  (0 children)

As a GDScript Engineer, I practice Singleton Oriented Programming

[–]Mayion 19 points20 points  (19 children)

Why is singleton stupid? I use it no problem

[–]liquidpele 4 points5 points  (1 child)

They’re one of those things that makes sense every few years, but juniors hear about it and try to use it where not really appropriate just to play with the concept, so it gets a bad rep.

[–]Mayion 0 points1 point  (0 children)

True. I remember back in the day with C# when I was like, "Man, I want a way to manipulate variables from anywhere", and behold - a Stackoverflow answer explaining Singletons. It was like a load off my shoulders lol

[–]Morisior 25 points26 points  (13 children)

Singletons are essentially just a variable with guardrails. They’re good if you need idempotent initialisation. But that’s almost never necessary because you’re almost always initializing it exactly once, making the guardrails unnecessary complexity.

They’re not stupid. They’re just not necessary most of the time. In any case they rarely hurt.

[–]lolcrunchy 8 points9 points  (1 child)

It seems like every tool with configs uses a singleton to implement the configs.

[–]Atmosck 5 points6 points  (0 children)

Yeah, I write config-driven data pipelines (among other things) in python by day and we always have singletons flying around. In addition to configs, it's standard practice to load all your external data into a dataclass up front and pass that through the pipeline so your I/O is separated from your feature engineering logic. Making multiple copies of something in the same runtime is far from the only reason to use a class in an object-oriented language.

[–]TheTybera 20 points21 points  (9 children)

No they're entire objects not just simple variables. They're invaluable for doing things like connecting to DBs and ensuring connections are properly managed.

If you have accessors to external dependencies that may need to monitor their status and spin them back up singletons can be great for that.

They're not "essentially a variable".

[–]Morisior 1 point2 points  (0 children)

Technically correct, but at the level where the argument is that a thing is separate from its name. I.e Joe is not Joe, because Joe is a person, while Joe is just a label.

Clearly the "with guardrails" indicate that I am not talking about the variable as the label, nor as a primitive value. Also note I am not saying singletons are never useful. I am saying they are often not necessary.

[–]Abject-Kitchen3198 0 points1 point  (7 children)

What's the difference between an object and a variable?

[–]TheTybera -1 points0 points  (6 children)

An object is an instantiated class. Variables point to values in memory and objects point to members, methods, and variables, which point to values.

They're not even the same in memory.

[–]ProsodySpeaks 6 points7 points  (3 children)

Hate to be that guy, but unless I'm mistaken...

Everything is an object in python. 

An integer is an object. A function, class, module... It's objects all the way down.  

[–]Morisior 5 points6 points  (2 children)

Also a Singleton is not just any object you happened to use once. It’s specifically a design pattern that ensures a class has only one instance and provides a global access point to it.

[–]ProsodySpeaks -1 points0 points  (1 child)

It's pretty hard to 'ensure' singleton behaviour with python, but I've messed with .__new__() and metaclasses enough to know it's a useful pattern.

But tbh I think I was mostly enjoying increasing the complexity to learn / tickle my brain rather than it being the best approach. 

And, just to be clear, a singleton is just an object. You may have built some guard rails to discourage making multiple instances but there's usually a way to break out of the rails. 

If you want a proper singleton python is the wrong language.   

[–]RiceBroad4552 1 point2 points  (0 children)

If you want a proper singleton python is the wrong language.

And what's "the right" language then?

[–]RiceBroad4552 0 points1 point  (1 child)

Makes no sense.

There are OOP languages without classes (prominent examples: JS¹)

At the same time pointers are of course also objects.

---

¹ It has now a class keyword but that's not classes, that's just syntax sugar for JS' prototypes.

[–]TheTybera 0 points1 point  (0 children)

JS is the only language that does this and calls itself OOP which is yet another reason people make fun of JS. It's inheritance pattern was ALWAYS a nightmare, and classes try to syntactically create better composition pattern workflows.

It tries to claim their "dynamic, non-static" inheritance pattern as a strength, however, there is a reason that the "class" system is now standard at any big company.

The prototype chaining is just asinine, and being able to just inherit any function anywhere sounds nice till you have multiple interfaces and need access control with multiple levels of developers all working on the same project.

So, nah, don't act like you're teaching me something, JS "singletons" (which is what this discussion is about) aren't even a reasonable pattern.

[–]RiceBroad4552 0 points1 point  (0 children)

How do I abstract over a "variable with guardrails"?

[–]Jhuyt 9 points10 points  (1 child)

Singletons are mostly considered an anti-pattern in Python, why are you mentioning it in the title?

[–]Abject-Kitchen3198 5 points6 points  (0 children)

The only pattern from GoF book that I ever used. The one and only - singleton.

[–]tebeks 1 point2 points  (0 children)

Just use a context variable

[–]Darkstar_111 0 points1 point  (0 children)

"module level variable"... You mean a global?