Python 似乎将实例属性视为 class 属性

Python seems to be treating instance attribute like a class attribute

首先我想说,我对 Python 和一般的编码还很陌生,所以我很可能使用了一些术语不正确。我正在尝试在 Python 中制作纸牌游戏 "Exploding Kittens" 的基本版本。我正在尝试将牌组中的牌(没有小猫,你想避免绘制的牌)发给 class 玩家的每个对象的实例属性 "hand",然后从牌组中取出牌。我的问题是,如果实例属性看起来不像 class 属性,我就无法制作实例属性。我的代码和结果如下所示:

import random

# Deck without kittens to deal out to the Players
deck_no_kittens = ["Attack","Attack","Attack","Attack","Steal","Steal","Steal","Steal","Favor","Favor","Favor","Favor","See the future","See the future","See the future","See the future","See the future","Alter the future","Alter the future","Shuffle","Shuffle","Shuffle","Shuffle","Skip","Skip","Skip","Skip"]

# Default starting hand for each player
start_hand = ["Defuse"]

class Player():
  def __init__(self, hand, alive, knowskitten):
    # Hand of each Player
    self.hand = hand
    # Determines if Player is alive
    self.alive = True
    # Determines if the Player knows if there is a kitten
    self.knowskitten = False

# Defines function that deals to Player while also removing it from deck_no_kittens
def deal(player):
  random_index = random.randint(0,len(deck_no_kittens)-1)
  card_to_add = deck_no_kittens.pop(random_index)
  player.hand.append(card_to_add)

# Initialize objects
computer1 = Player(start_hand, True, False)
computer2 = Player(start_hand, True, False)
user = Player(start_hand, True, False)

# Below is where my issue seems to be - the hand remains the same throughout each deal

# Deals 5 times, alternating, to computer1 and computer2, and prints the hand each time
for i in range(5):
  deal(computer1)
  print("\ncomputer1 hand is "+str(computer1.hand))
  deal(computer2)
  print("\ncomputer2 hand is"+str(computer2.hand))

# Prints deck_no_kittens
print("\n"+str(deck_no_kittens))

结果:

computer1 hand is ['Defuse', 'Attack']

computer2 hand is['Defuse', 'Attack', 'Skip']

computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future']

computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal']

computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip']

computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack']

computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack']

computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle']

computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor']

computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor', 'Shuffle']

['Attack', 'Steal', 'Steal', 'Steal', 'Favor', 'Favor', 'Favor', 'See the future', 'See the future', 'See the future', 'See the future', 'Alter the future', 'Alter the future', 'Shuffle', 'Shuffle', 'Skip', 'Skip']

我预计每个对象的每只手都会不同,但每笔交易都会增加通用手。任何 help/suggestions 都表示赞赏。

两个实例的 hand 属性都是对同一个列表的引用,因此当修改该列表时,它会影响两者。

一个简单的解决方法是将列表复制到 class 的 __init__:

from copy import copy

class Player():
  def __init__(self, hand, alive, knowskitten):
    # Hand of each Player
    self.hand = copy(hand)
    # Determines if Player is alive
    self.alive = True
    # Determines if the Player knows if there is a kitten
    self.knowskitten = False

这里的问题是您使用相同的 "object" start_hand 来创建两个播放器。 您将对同一列表 start_hand 的引用存储在内部变量 hand.

当您在一个播放器中修改 hand - 其他播放器可以看到它。

要解决此问题,请创建一个新列表。

self.hand = list(start_hand)