I’m new to Python and I’m not sure what’s the best place to put my user-defined exceptions, and where to catch them.
For example, I have a program which starts a game, when the game is stopped, an user-defined exception is raised (StopGame). I should catch this exception from outside the game since the game is throwing this exception. Though, the exception is specific to the game so the definition of the exception should be in the game module (or maybe the game package?).
Here is the simple layout of the program:
Program structure:
__main__.py
game/
__init__.py
game.py
__main__.py
import game
def main():
g = game.Game()
try:
g.start()
except game.StopGame:
print '\nbye bye!'
if __name__ == '__main__':
main()
game/__init__.py
from game import Game
from game import StopGame
game/game.py
class Game:
def start(self):
try:
a = raw_input('do you want to play?\n')
print 'cool'
except (KeyboardInterrupt, EOFError):
raise StopGame
if a == 'no':
raise StopGame
else:
print 'cool'
class StopGame(Exception):
pass
This code works fine like this, I’m just not sure this is the way to go. What I find somewhat disappointing is that I should import every exception in game/_init_.py using this setup. Otherwise I should catch the exception like:
except game.game.StopGame
Which looks a bit nasty. I think it’s better to be able to reach all the attributes in the module ‘game’ from the variable ‘game’ in _main_.py. Then I know you can put this in game/_init_.py:
from game import *
Which will import all classes from game/game.py, but I heard that using * for importing is a bad practice.
So, my question is, what is the best way to do this? Maybe my whole setup is wrong, if so, I would like to hear what’s the correct setup.
Thanks a lot in advance!
BTW: If you might wonder why I have a game module inside a game package: the idea is that the package will contain a lot of more modules that are related to the game, game/game.py is just the initiator of the whole game, maybe I should put that code into game/_init_.py?
How is it done elsewhere?
I think that the best way to answer your issue is to look at an actual Python package and see how it’s built. I’ll take the example of the excellent python-requests package.
This module is about making HTTP requests. Yours is about playing a game.
Basic HTTP requests functionality is imported in the
requests/__init__.pyfile, but is defined elsewhere. That’s what you’re doing, but the ‘elsewhere’ could have a better name. Maybegame/core.pycould be a good fit.Exceptions are defined in the
requests/exceptions.pyfile. It’s usually appropriate for a relatively-small package to have all the exceptions in one place.Note that the exceptions are imported into
requests/__init__.pytoo! This makes them easier to import in other packages that might need to catch them!Last, no
from module import *is used. It’s not going to take so much time to actually add exactly what’s needed, so you should avoid*.What can you do in your case?
game/game.pyfile, call itgame/core.pyor somethinggame/exceptions.pyfilegame/__init__.py, import what’s usually needed from your package: the main classes and the exceptions.