0

Simple question. What would be the most pythonic way to make a class unable to be instantiated.

I know this could be done by overriding new __new__ and raising an exception there. Is there a better way?

I am kinda looking in the abstract class direction. However, I will NOT use this class as a base class to any other classes. And will NOT have any abstract methods defined in the class.

Without those, the abstract class does not raise any exceptions upon instantiation.

I have looked at this answer. It is not what I am looking for. Is it possible to make abstract classes in Python?

Edit:

I know this might not be the best approach, but it is how I solved something I needed. I think it is a simple question, that can have a yes + explanation or no answer. Answers/comments that basically amount to, you should not be asking this question make very little sense.

SirSteel
  • 123
  • 1
  • 10
  • 7
    What is the use case of such a class? – chepner Feb 05 '18 at 15:06
  • I will have it as a collection of some `@classmethods`, which need some settings, which do not really change. – SirSteel Feb 05 '18 at 15:09
  • 7
    That's not what classes are for. Put those methods into a module instead. (This isn't java!) – Aran-Fey Feb 05 '18 at 15:10
  • 3
    If all you want to do is add a namespace to a set of functions, why not just keep them as functions in a module. You can access the functions through the module name. – hspandher Feb 05 '18 at 15:11
  • Thank you for your comments. I do understand what you mean, I could do it that way, but I decided to do it differently, since there are settings, that might change, although not often. I am looking for an answer to the original question, not an answer to the question should I even do it anyways :) – SirSteel Feb 05 '18 at 15:18
  • If there might be settings, then those should be module-level global variables. There is no need to define a class here. – chepner Feb 05 '18 at 15:49
  • 1
    It is quite possible to define abstract classes, but you aren't asking about them. You are asking about an "absurd" class, because there is nothing class-like about how you intend to use it. – chepner Feb 05 '18 at 15:50
  • True, that could be. But that seems to complicate changing those settings dynamically, at runtime, if the need presents itself. Currently the solution works fine as I intend it. And the question I asked can be thought of as a curiosity question. I do not really **need** the class to be uninstantiable. – SirSteel Feb 05 '18 at 15:52
  • I know about abstract class option, and I mentioned in my post, that it does not seem to block instantiation without defining an abstract method. Absurd it is, I mean, I asked a question, I don't remember asking for an opinion. I understand it might not be the best way, but that's hardly the point. – SirSteel Feb 05 '18 at 15:56
  • 1
    you asked for "the most pythonic way" to group functions under a namespace. people have answered that the pythonic solution is to put them in a module. the only other possible answer is that there is no such "most pythonic way" because what you're talking about isn't even a class. – apteryx Feb 05 '18 at 16:31

1 Answers1

0

what you're after is being able to just call something like, math.sin(x) right?

you make it a static method. and you just simply don't make any instances.

@staticmethod
def awesome_method(x): return x

if you're thinking why can't you actually do a = math() because math, isn't a class. it's a separate script with all those functions in it.

so you may want to make a separate script with all your static things in it. ;)

if you want everything in the same script, then it's just local variables. and if you're like me, you'd rather them be organised properly with classes, looks like you'd probably be best just making a normal class, and creating just one instance of it

Puddle
  • 2,993
  • 1
  • 19
  • 32
  • No, I am after a class that cannot be instantiated. I know the staticmethod option, it is not what I need/want. – SirSteel Feb 05 '18 at 15:22
  • 2
    well, your __new__ raise error way is the only way about that. and tell me this, why do you need it to be uninstantiable? are you ever going to try instantiate it? what's the issue if you... just know... you could instantiate it? i'd say, just get on with coding with what you need. or tell us what you're coding that needs an uninstantiable class. – Puddle Feb 05 '18 at 15:27
  • Yeah, I don't need a singleton either. I was just asking a question, if the answer is no, the `__new__` override is the only way to do it, that's a good enough answer. I do not see a reason why people would continue trying to convince me I do not need or should not do it this way, after I have stated I understand the situation and wish to do it that way. – SirSteel Feb 05 '18 at 15:43
  • i understand the situation, in java i use static classes, methods, fields a bit myself. it's nice and easy. but python is just the way it is. you was simply asking a pointless question. how to make it so you can't instantiate it at all. who's going to need that? if you're not going to instantiate in the first place? you know how to make static variables and modules. isn't that good enough? not all languages are the same. – Puddle Feb 05 '18 at 15:51
  • I have never used Java in my life. I don't see why a simple question, is pointless. I should not do it this way? Ok, got it, thank you. Your answer was no, the `__new__` override is the only way, if nobody suggests any other options, that's what I will go with. – SirSteel Feb 05 '18 at 16:03
  • 2
    because this particular simple question, was a pointless one. i've asked you if you're looking for something like math.sin(x), and you said, "No, I am after a class that cannot be instantiated... it is not what I need/want." so what even is your goal? you haven't said. you're wasting your own, and other peoples time. what could you want, with a door that cannot be opened? what would you do next? if you got an answer to this? – Puddle Feb 05 '18 at 16:11
  • Anyone that feels this question is a waste of their time, is free to skip it. I am not forcing anyone to answer it. It's a volunteer transaction, like everything on SO. To me it seems much more like you are wasting your time trying to get your point of pointlessness across. Like I said, I got it. You can use your time more productively as you desire, I am not forcing you to give any more thought to this question. – SirSteel Feb 05 '18 at 16:34