从同一类中的另一个def打印def

2024-04-26 05:50:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在打印def中的代码。 结果不是得到print,而是得到: <function Partie.afficher_etat_donnes at 0x000000FF03B4C950>

代码如下:

class Partie:
    def __init__(self, plateau, donnes):
        self.plateau = plateau
        self.donnes = donnes
        self.tour = None
        self.passe = 0
        self.gagnant = None
    @staticmethod
    def show_instructions():
        instructions = """

    Game Instructions :
            """
        print(instructions)
        print(Partie.afficher_etat_donnes)

    def afficher_etat_donnes(self):
        for joueur in range(self.nombre_joueurs):
            print(f"The player {joueur} has {len(donnes[joueur])} dominos in hand.")

对于重要的变量。。。在这种情况下: joueur = 2 donnes = [[3,1],[3,2]],[[6,6],[6,3] Donnes只是一个例子。 因此,我应该: The player 0 has 2 dominos in hand.


Tags: the代码inselfnonedefplayerprint
1条回答
网友
1楼 · 发布于 2024-04-26 05:50:01

像这样试试吧-我不会说法语,也不会玩domino,但这段代码在某种程度上起作用:

class Partie:
    def __init__(self, plateau, donnes):
        self.plateau = plateau
        self.donnes = donnes
        self.tour = None
        self.passe = 0
        self.gagnant = None
        self.nombre_joueurs = 3  # added


    @staticmethod
    def show_instructions(somePartie):
        instructions = """

    Game Instructions :
            """
        print(instructions)
        somePartie.afficher_etat_donnes()  # changed, needs instance of partie to call 
                                           # non static method on the instance....


    def afficher_etat_donnes(self):
        for joueur in range(self.nombre_joueurs):
            # fixed missing self.
            print(f"The player {joueur} has {len(self.donnes[joueur])} dominos in hand.") 

# make a Partie
p = Partie("",[[1,1,1,1],[2,2,2,2,2,2],[2,2,2]])
# call static method, give it instance of partie to enable calling other method
Partie.show_instructions(p)

输出:

    Game Instructions :

The player 0 has 4 dominos in hand.
The player 1 has 6 dominos in hand.
The player 2 has 3 dominos in hand.

相关问题 更多 >