I am currently writing my python classes and instantiate them like this
class calculations_class():
def calculate(self):
return True
Calculations = calculations_class()
I was wondering if I was doing this correctly, or if there were any other ways to instantiate them. Thanks!
Apart from the naming convention issue which other answers have correctly pointed out, you’re basically fine: calling a class is indeed by far the most common way of instantiating that class. If you need any per-instance initialization (most typically setting some instance-attributes to initial values), be sure to define an
__init__method that performs it:The other, rare ways of instantiating a class typically occur when you want to bypass the initialization part for some reason (e.g., in the course of de-serializing an instance from some file, database, or communication from other processes — the
picklemodule is a good example of needing such advanced approaches). I don’t think you should worry about them at all at this stage of your Python learning experience.