繁体   English   中英

将 arguments 传递给 class 方法混淆

[英]Passing arguments to class methods confusion

我对使用类进行编程是全新的。 我想创建一种方法,它只需要一个 class 变量并且不知道它是否可能,更不用说如何去做。 我的代码运行良好,但似乎应该有一个更优雅的解决方案。 正如您在下面的代码中看到的那样,实际上重复了相同的代码,主要区别在于检查的是“player_pieces”还是“pc_pieces”。 所以我想创建一个单独的方法,但我不知道如何挑出一个 class 变量传递给它。

下面的代码是在玩家或电脑的棋子中找到最高的双打。

       doubles = [[6, 6], [5, 5], [4, 4], [3, 3], [2, 2], [1, 1], [0, 0]]

                for double in doubles:
                if double in self.player_pieces:
                    self.status = "computer"
                    self.domino_snake.append(double)
                    self.player_pieces.remove(double)
                    doubles_in_hand = True
                    break
                elif double in self.pc_pieces:
                    self.status = "player"
                    self.domino_snake.append(double)
                    self.pc_pieces.remove(double)
                    doubles_in_hand = True
                    break

我知道这是一个非常基本的问题,我可能错过了一些愚蠢的东西,但任何帮助将不胜感激。

首先,您可以将重复的代码放在单独的function中。

其次,我还将for循环更改为while循环 - 如果double_in_handTrue (或者如果我们完成了所有doubles数的运行),循环将退出。

您还可以使double_in_hand变量成为 class 实例的一部分。 然后,您可以在循环之前将其初始化为False ,如果满足条件,则将其更新为True

您还可以在另一个 function 中设置status变量,因为该逻辑也是重复的:

def another_function(self, double, status):
    self.status = status
    self.domino_snake.append(double)
    self.player_pieces.remove(double)
    self.double_in_hand = True


def your_function(self, doubles):
    self.double_in_hand = False
    while not self.double_in_hand and index < len(doubles):
        if doubles[index] in self.player_pieces:
            self.another_function(doubles[index], "computer")
        elif doubles[index] in self.pc_pieces:
            self.another_function(doubles[index], "player")
        index += 1
        

此外,变量名doublesdouble没有提供信息,您应该考虑重命名它们。

从一个列表中删除一个元素并将其附加到另一个列表中基本上是移动的。 让我们创建一个负责那件事的方法:

def move_to_snake(self, pieces, double):
    self.domino_snake.append(double)
    pieces.remove(double)
    return True

并在您的代码中使用它:

for double in doubles:
    if double in self.player_pieces:
        self.status = "computer"
        doubles_in_hand = self.move_to_snake(self.player_pieces, double)
    elif double in self.pc_pieces:
        self.status = "player"
        doubles_in_hand = self.move_to_snake(self.pc_pieces, double)

    if doubles_in_hand:
        break

首先,您拥有代码还不错。 保持原样还不错。
不过,如果你想删除重复的代码,你可以使用double_in_hand检查重复的代码是否需要在 if/else 块之后运行:

for double in doubles:
    if double in self.player_pieces:
        self.status = "computer"
        doubles_in_hand = True
    elif double in self.pc_pieces:
        self.status = "player"
        doubles_in_hand = True
    
    if double_in_hand:
        self.domino_snake.append(double)
        self.player_pieces.remove(double)
        break

*我假设double_in_handFalse开头。
*我假设语句的顺序无关紧要,例如doubles_in_hand = Trueself.domino_snake.append(double)之前运行并不重要


go 关于它的另一种方法是在添加的else块中使用continue 这种方式比较复杂,所以我不喜欢它,但它是一种选择。

for double in doubles:
    if double in self.player_pieces:
        self.status = "computer"
    elif double in self.pc_pieces:
        self.status = "player"
    else:
        continue
    
    self.domino_snake.append(double)
    self.player_pieces.remove(double)
    doubles_in_hand = True
    break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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