问题描述
我有一个叫团队名单,其中包含两个对象,这些都是同一个类的对象,他们都有一个成员列表中。我追加到这些名单分别。见Fight.AddParticipant但我追加两个参与者物体似乎在这两个球队的对象,这是意外的行为来结束。为什么会出现这种情况?
code:
类战斗:
参与者= []
团队= []
攻击= []
战斗= 0 高清MakeTeams(个体经营,队):
self.teams.append(队) 高清NUMPARTICIPANTS(个体经营,teamnum =无):
如果(teamnum =无!):
返回LEN(self.teams [teamnum] .members)
其他:
返回LEN(self.participants)
高清AddParticipant(个体经营者,参加,队):
self.participants.append(参与者)
REF = self.participants [-1]
self.teams [队伍] .members.append(REF)
#打印self.teams [1] .members [0]。名称 高清SetFighting(个体经营):
self.fighting = self.NumParticipants() 高清AnnounceFight(个体经营):
打印之间的战斗,self.NumParticipants(),战士开始了!\\ n \\ n'
self.AnnounceTeams() 高清AnnounceTeams(个体经营):
打印''
球队在self.teams:
打印团队名称:team.name
打印团队的士气:team.morale
会员在team.members:
打印member.name
类参与者:
NAME =
种族=
性别=
HP = 0
强度= 0
敏捷= 0
武器=
活着= TRUE 高清__init __(自我,名称,种族,性别,HP,力量,敏捷,武器,活着= TRUE):
self.name =名称
self.race =赛
self.sex =性爱
self.hp =马力
self.strength =实力
self.agility =敏捷
self.weapon =武器
self.alive =活着
一流的团队:
NAME =
成员= []
士气= 0 高清__init __(个体经营,名称,士气):
self.name =名称
self.morale =士气扑灭扑灭=()
Fight.MakeTeams(团队('智能',1))
Fight.MakeTeams(团队('阿呆',1))
Fight.AddParticipant(参与者(富,人,女,15,15,20,刀),0)
Fight.AddParticipant(参与者(酒吧,人,男性,15,15,20日,记),1)
Fight.SetFighting()
Fight.AnnounceFight()
在所有的类,你要初始化这样的实例变量:
高清__init __(个体经营):
self.participants = []
self.teams = []
self.attacked = []
self.fighting = 0
这样,他们是独立的每个战斗,参与者,团队,而不是对所有的战斗,参加者或团队共享。
I have a list called teams which contains two objects, these are objects of the same class and they both have a "members" list. I am appending to these lists individually. See Fight.AddParticipant but the two participant objects I'm appending seem to end up in both of the team objects, which is unintended behavior. Why is this happening?
Code:
class Fight:
participants = []
teams = []
attacked = []
fighting = 0
def MakeTeams(self, team):
self.teams.append(team)
def NumParticipants(self, teamnum = None):
if (teamnum != None):
return len(self.teams[teamnum].members)
else:
return len(self.participants)
def AddParticipant(self, participant, team):
self.participants.append(participant)
ref = self.participants[-1]
self.teams[team].members.append(ref)
# print self.teams[1].members[0].name
def SetFighting(self):
self.fighting = self.NumParticipants()
def AnnounceFight(self):
print 'A battle between', self.NumParticipants(), 'fighters has begun!\n\n'
self.AnnounceTeams()
def AnnounceTeams(self):
print ''
for team in self.teams:
print "Team name:", team.name
print "Team morale:", team.morale
for member in team.members:
print member.name
class Participant:
name = ""
race = ""
sex = ""
hp = 0
strength = 0
agility = 0
weapon = ""
alive = True
def __init__(self, name, race, sex, hp, strength, agility, weapon, alive = True):
self.name = name
self.race = race
self.sex = sex
self.hp = hp
self.strength = strength
self.agility = agility
self.weapon = weapon
self.alive = alive
class Team:
name = ""
members = []
morale = 0
def __init__(self, name, morale):
self.name = name
self.morale = morale
Fight = Fight()
Fight.MakeTeams(Team('Smart', 1))
Fight.MakeTeams(Team('Dumb', 1))
Fight.AddParticipant(Participant("Foo", "Human", "Female", 15, 15, 20, "Knife"), 0)
Fight.AddParticipant(Participant("Bar", "Human", "Male", 15, 15, 20, "Sabre"), 1)
Fight.SetFighting()
Fight.AnnounceFight()
In all of your classes, you want to initialize instance variables like this:
def __init__(self):
self.participants = []
self.teams = []
self.attacked = []
self.fighting = 0
That way, they are separate for each fight, participant, team instead of shared for all fights, participants, or teams.
这篇关于Python的追加两个名单时,它应该只追加一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!