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

问题描述 投票:1回答:2

我想通过说我对Python和编码一般来说很新,所以我很可能错误地使用了一些术语。我正在尝试用Python制作纸牌游戏“Exploding Kittens”的基本版本。我试图将卡片从一个牌组(没有小猫,你想要避免绘制的牌)交给一个实例属性“手”给每个玩家类别的对象,然后从牌组中取出牌。我的问题是,如果没有它看起来像是一个类属性,我就无法创建实例属性。我的代码和结果显示如下:

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']

我预计每个对象的每只手都会有所不同,但每笔交易都会增加一个普遍的手。任何帮助/建议表示赞赏。

python class attributes instance
2个回答
0
投票

这里的问题是你使用相同的“对象”start_hand来创建两个玩家。您在内部变量start_hand中存储对相同列表hand的引用。

当你在一个玩家中修改hand时 - 其他玩家可以看到它。

要解决此问题,请改为创建新列表。

self.hand = list(start_hand)

0
投票

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

一个简单的解决方法是复制班级__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
© www.soinside.com 2019 - 2024. All rights reserved.