将模块名称以外的其他项目放在包的__all__变量中是不好的做法吗?

时间:2016-12-28 04:15:41

标签: python python-3.x packages

我正在创建一个数字版本的"三角钉游戏" (使用Python 3.6.0在Cracker Barrel中常见的那个)。我包括一个名为" stats"的模块。这允许游戏分析所有可能的游戏,以便当玩家按下帮助按钮时,它将给予他最好的移动。

虽然这个项目主要是一个应用程序,但我希望源代码也可用,以便其他人可以阅读或使用它。为此(以及我用于创建游戏),我已经包含了两个图形类。第一个,使用乌龟,将在有人实际玩游戏时使用,第二个,使用ASCII来提高速度和效率,可用于统计测试。我创建了一个名为" graphics"的子包,每个类都位于" graphics"内的自己的模块中。经过深思熟虑后,我决定将基类放在__init__模块中。另外,因为我希望它的__all__变量包含对使用源代码的人有用的所有内容,所以我将基类的名称放在其中。

这里是__init__模块:

"""Initializes the 'graphics' subpackage."""

__all__ = ["Graphics", "ascii_", "turtle_"]

class Graphics:
    """Base class for all graphics classes.

    instance attributes
    -------------------
    '_game' -- the instance of 'game.Game' that the object is assigned to.
    This attribute is not assigned until the game is begun.
    """

    def _construct(self):
        """Constructs the graphics for the game."""
        pass

    def _update(self):
        """Updates the graphics when a move is made."""
        pass

    # Move back to ASCIIGraphics if not necessary for other graphics
    # classes.
    def _erase(self):
        """Updates the graphics when a move is undone."""
        pass

    def _reset(self):
        """Resets the graphics when the game is restarted."""
        pass

包的__all__变量是否应该只包含模块名,或者它是否也包含__init__中定义的基类/包级全局变量的名称?

P.S。:我非常清楚[package | module] import *""许多人不赞同,我已经研究过是否应该在__init__中定义基类。话虽这么说,这是我第一次用Python编写一个包,如果你想给我建议并回答我的问题,请随意这样做。

0 个答案:

没有答案