小编Tim*_*ger的帖子

是一个无限循环的坏练习?

我正在用Python实现一个纸牌游戏,为了我的班级来处理玩家PlayerHandler,我最近实现__next__了简单地打电话next_player.因为游戏玩法可以被认为是无限循环(玩家将继续玩,直到他们退出或赢/输),它停止迭代是没有意义的.但是,如果for循环导致无限循环,那么它可能会让人感到困惑,所以我应该在StopIteration某处提升?

class PlayerHandler():
    """Holds and handles players and order"""
    def __init__(self, players=None, order=1):
        if players is None:
            self.players = []
        else:
            self.players = list(players)
        self.current_player = None
        self.next_player()
        self.order = order

    def get_player(self, interval):
        assert type(interval) == int, "Key must be an integer!"
        if not interval and self.players:
            return self.current_player
        elif self.players:
            step = interval * self.order
            index = self.players.index(self.current_player) + step
            while index >= len(self.players):
                index -= len(self.players)
            while index <= …
Run Code Online (Sandbox Code Playgroud)

python iterator loops

5
推荐指数
1
解决办法
243
查看次数

为什么发生恐慌时析构函数会运行?

如果 Rust 程序发生恐慌,并且假设没有恐慌捕捉器(有一段时间没有),那么不运行析构函数并让操作系统在该过程后进行清理肯定是安全且良好的。Rust 为什么要展开线程?

我能想到的唯一原因是当没有操作系统来回收内存时,但除了这个利基之外,它似乎没有必要。

destructor memory-management rust exception-safety

1
推荐指数
1
解决办法
635
查看次数