简体   繁体   中英

Python user-defined exceptions placement and catching

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__.py file, but is defined elsewhere. That's what you're doing, but the 'elsewhere' could have a better name. Maybe game/core.py could be a good fit.

Exceptions are defined in the requests/exceptions.py file. 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__.py too! 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?

  • Avoid having a game/game.py file, call it game/core.py or something
  • For a small package, put your exceptions in a game/exceptions.py file
  • In game/__init__.py , import what's usually needed from your package: the main classes and the exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM