Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.
There is a similar questionquestion, but it has a different focus:
- it is about grouping global variables and functions into a class and
- It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.
UPDATE:
In Python, I can do the following with both classes and objects:
class A(object):
pass
A.some_state_var = True
# Then later
A.some_state_var = False
my_a = A()
my_a.some_state_var = True
# Then later
my_a.some_state_var = False
So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.
Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.
Also, it is not so much about creating a new Type.
The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering whether to instantiate this class or not.