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

时间:2018-04-01 17:03:13

标签: python python-3.x

我正在尝试打印我在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.

1 个答案:

答案 0 :(得分:0)

尝试这样 - 我不会说法语,也不会玩多米诺骨牌,但这段代码有点作用:

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.