Can I add modules and special methods to modules? I want to define a module so that its import is executed as an instance of the class, and the body acts as a class definition. Essentially, to avoid the ugly syntax, for example:
import game if game.Game().paused: print("The game is paused")
eg. the game module will 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 (e.g. __call__
)?
To be clear, I don't distinguish between class / instance methods since I use game.Game
as a singleton / borg class.
I tested using @property and defining __bool__
, but not in the way I had hoped.
Edit (information on why I want to use the property):
I have a game.speed
property, a game.paused()
function, and a game.pause(bool)
function. Essentially, I have a temporary variable used to store the speed of the game 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 that the speed is zero, and be able to change the speed while the game is paused, so that when the game resumes, it uses the new speed.
source share