Is it possible to add properties and special methods to modules? I want to define a module such that importing it acts like a class instance, and the body acts as a class definition. Essentially, it's to avoid ugly syntax like this:
import game
if game.Game().paused:
print("The game is paused")
E.g. the game module would look like this:
_Speed = 1
@property
def paused():
return _Speed == 0
And the file using it:
import game
if game.paused:
print("The game is paused")
Also, is it possible to define special methods (such as __call__
)?
To be clear, I do not differentiate between class/instance methods, since I'm using game.Game
as a singleton/borg class.
I have tested using @property and defining __bool__
, but neither acts as I hoped.
Edit (information on why I want to use a property):
I have a property game.speed
, a function game.paused()
and a function game.pause(bool)
. Essentially, I have a temporary variable used to store the game speed when the game is paused. There is a private speed variable that is set to zero when the game is paused. I never want the user to see the speed as being zero, and be able to modify the speed while the game is paused, so that when the game is resumed, it uses the new speed.