【问题标题】:Managing Instances in Python在 Python 中管理实例
【发布时间】:2010-06-03 02:34:39
【问题描述】:

我是 Python 新手,这是我第一次提出 stackOverflow 问题,但我是老读者了。我正在开发一个简单的基于卡片的游戏,但在管理我的手类的实例时遇到了麻烦。如果你看下面,你会看到手类是一个简单的卡片容器(只是 int 值),每个玩家类都包含一个手类。但是,每当我创建 Player 类的多个实例时,它们似乎都在操纵 Hand 类的单个实例。根据我在 C 和 Java 方面的经验,我似乎以某种方式使我的 Hand 类成为静态的。如果有人可以帮助解决这个问题,我将不胜感激。

谢谢你, 萨德

澄清一下:这种情况的一个例子是

p = player.Player()
p1 = player.Player()
p.recieveCard(15)
p1.recieveCard(21)
p.viewHand()

这将导致: [15,21]
即使只有一张卡被添加到 p

手类:

class Hand:

    index = 0
    cards = [] #Collections of cards

    #Constructor
    def __init__(self):
        self.index
        self.cards

    def addCard(self, card):
        """Adds a card to current hand"""
        self.cards.append(card)
        return card

    def discardCard(self, card):
        """Discards a card from current hand"""
        self.cards.remove(card)
        return card

    def viewCards(self):
        """Returns a collection of cards"""
        return self.cards

    def fold(self):
        """Folds the current hand"""
        temp = self.cards
        self.cards = []
        return temp

播放器类

import hand
class Player:

 name = ""
 position = 0
 chips = 0
 dealer = 0
        pHand = []

 def __init__ (self, nm, pos, buyIn, deal):
            self.name = nm
            self.position = pos
            self.chips = buyIn
            self.dealer = deal

            self.pHand = hand.Hand()
            return

        def recieveCard(self, card):
            """Recieve card from the dealer"""
            self.pHand.addCard(card)
            return card

        def discardCard(self, card):
            """Throw away a card"""
            self.pHand.discardCard(card)
            return card

        def viewHand(self):
            """View the players hand"""
            return self.pHand.viewCards()

        def getChips(self):
            """Get the number of chips the player currently holds"""
            return self.chips

        def setChips(self, chip):
            """Sets the number of chips the player holds"""
            self.chips = chip
            return

        def makeDealer(self):
            """Makes this player the dealer"""
            self.dealer = 1
            return

        def notDealer(self):
            """Makes this player not the dealer"""
            self.dealer = 0
            return

        def isDealer(self):
            """Returns flag wether this player is the dealer"""
            return self.dealer

        def getPosition(self):
            """Returns position of the player"""
            return self.position

        def getName(self):
            """Returns name of the player"""
            return self.name

【问题讨论】:

    标签: python static class initialization instances


    【解决方案1】:

    根据我在 C 和 Java 方面的经验,我似乎正在以某种方式将 Hand 类设为静态。

    实际上,这基本上就是您正在做的事情。好吧,并不是真正将类设为静态,而是将变量设为静态。

    当你这样写声明时:

    class Hand:
        cards = []
    

    该变量 (cards) 与 类 相关联,而不是与实例相关联。为了类比 Java,Python 类中不属于该类方法的每个语句基本上都在静态初始化程序中运行。你几乎可以这样想:

    class Hand {
        static {
            cards = new object[];
        }
    }
    

    (当然只是一个粗略的类比)

    要在 Python 中创建实例变量,您必须将其设置为实例的属性,这需要您等待,直到您有对该实例的引用。实际上,这意味着您在构造函数中对其进行初始化,如下所示:

    class Hand:
        def __init__(self):
            self.cards = []
    

    【讨论】:

    • 非常感谢!我无法告诉你这有多大帮助。
    【解决方案2】:

    你的问题很简单

    如果您将列表分配给 python 类的主体,当您将项目附加到它时,它们将存储在类级别,而不是实例级别。

    你可以通过添加以下行来解决这个问题:

    def __init__(self):
        self.cards = []
    

    这是一个众所周知的 python 陷阱案例,我推荐你阅读: http://zephyrfalcon.org/labs/python_pitfalls.html

    【讨论】:

      【解决方案3】:

      正如其他答案所述,您对类变量与实例变量感到困惑。我建议你回顾一下 Python 类是如何工作的基础知识。这是我为另一个问题写的答案;阅读本文可能会对您有所帮助。

      How to define a class in Python

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2015-10-22
        • 2016-05-31
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        相关资源
        最近更新 更多