首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python似乎将实例属性视为类属性

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

Stack Overflow用户
提问于 2019-04-16 20:47:52
回答 2查看 41关注 0票数 1

我想在开始之前说,我对Python和编码是非常陌生的,所以我很可能使用了一些不正确的术语。我正在尝试用Python制作一个基本版本的纸牌游戏“炸开小猫”。我正在尝试从一副牌(没有小猫,你想要避免绘制的牌)到每个类玩家的对象的实例属性"hand“中发牌,然后从牌组中移除这张牌。我的问题是,如果不让实例属性看起来像一个类属性,我就不能让它变成hand。我的代码和结果如下所示:

代码语言:javascript
运行
复制
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))

结果:

代码语言:javascript
运行
复制
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']

我预计每个对象的每一只手都是不同的,但每一笔交易都会增加一只通用的手。如有任何帮助/建议,我们不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-16 20:54:45

这里的问题是,您使用相同的“对象”start_hand来创建两个播放器。您可以在内部变量hand中存储对同一列表start_hand的引用。

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

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

代码语言:javascript
运行
复制
self.hand = list(start_hand)
票数 0
EN

Stack Overflow用户

发布于 2019-04-16 20:53:44

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

一种简单的解决方法是将列表复制到类的__init__

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55708537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档