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.
It works fine. Both the function calls would be executed and we get the results. We have to be careful if two modules has same function with different parameters while importing.
Module property provides the default value for a module. Multiple process properties can source their value from a single module property. Module properties are defined at the module level and can be referenced by various resources that are defined as a part of the module.
A module can only contain declarations and functions to be used by other modules and programs. This is perhaps one of the most important difference. As a result, a module cannot exist alone; it must be used with other modules and a main program.
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names, if placed at the top level of a module (outside any functions or classes), are added to the module's global namespace.
Python doesn't care that what's in sys.modules
is actually a module. So you can just:
# game.py
class Game(object):
pass
import sys
sys.modules["game"] = Game()
Now other modules that import game
will get the Game
instance, not the original module.
I'm not sure I recommend it, but it'll do what you want.
If all you are looking for is the syntax as you mentioned, then you can define a class and use class level attributes
a1.py
class Game(object):
paused = True
>>> from a1 import Game
>>> Game.paused
True
>>> Game.paused = False
>>> Game.paused
False
Well, as you asked about the Properties on Class, then you can do something with property decorator and classmethod. Soemthing like this
class ClassProperty(property):
def __get__(self, cls, owner):
return self.fget.__get__(None, owner)()
class Game(object):
stage = True
@ClassProperty
@classmethod
def paused(cls):
return Game.stage == True
>>> from a1 import Game
>>> Game.paused
True
It seems you want to avoid accessing items via the module. That's fairly easy to do.
These two are equivalent:
import game
if game.Game().paused:
print("The game is paused")
from game import Game
if Game().paused:
print("The game is paused")
Ok then, how about this:
# game.py
class Game(object):
@property
def paused():
return True
game = Game()
# from your module
from game import game
game.paused
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With