Can modules have properties?

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.

+6
source share
4 answers

Python doesn't care what sys.modules is actually a module. So you can simply:

 # game.py class Game(object): pass import sys sys.modules["game"] = Game() 

Now, other modules that import game will receive an instance of Game , rather than the original module.

I'm not sure I recommend it, but it will do what you want.

+11
source

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, when you asked about Class Properties , you can do something using the property decorator and class method. Something like that

 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 
+3
source

It seems you want to avoid accessing elements through the module. This is pretty easy to do.

These two equivalents:

 import game if game.Game().paused: print("The game is paused") from game import Game if Game().paused: print("The game is paused") 

So how about this:

 # game.py class Game(object): @property def paused(): return True game = Game() # from your module from game import game game.paused 
+2
source

I do not think this will work. You will need to import the module more than once, and Python will execute its contents several times. And again it will reset variable speed. I think you better use one class of the game that stores the state of the game. It will also keep your code much clearer and more convenient.

0
source

Source: https://habr.com/ru/post/912016/


All Articles