python - 如何在不更改父类变量本身的情况下更改子类中父类中定义的变量?
问题描述
我正在尝试基于同一个父类创建两个子类,以便它们每个都有自己的父对象中定义的相同变量的版本。但是我意识到,在其中一个子类中更改这些变量将导致另一个子类中的版本也发生更改。我知道我可能没有完全理解继承的概念。请帮忙!
import random
class PlayerParent():
id = 1
# Cooperate: True; Betrayal: False
opponent_moves_history = {}
self_moves_history = {}
def append_opponent_history(self, round_num, c_true, misunderstand=0.0):
# randomly change the result based on probability given in misunderstand
random_num = random.uniform(0, 1)
if random_num <= misunderstand:
c_true = not c_true
self.opponent_moves_history[round_num] = c_true
def append_self_history(self, round_num, c_true, misunderstand=0.0):
# randomly change the result based on probability given in misunderstand
random_num = random.uniform(0, 1)
if random_num <= misunderstand:
c_true = not c_true
self.self_moves_history[round_num] = c_true
score = int(0)
def score_keeper(self, round_num):
if (self.opponent_moves_history[round_num] == True) and (self.self_moves_history[round_num] == False):
self.score += 7
if (self.opponent_moves_history[round_num] == True) and (self.self_moves_history[round_num] == True):
self.score += 5
if (self.opponent_moves_history[round_num] == False) and (self.self_moves_history[round_num] == True):
self.score += 1
if (self.opponent_moves_history[round_num] == False) and (self.self_moves_history[round_num] == False):
self.score += 2
def get_score(self):
return self.score
class TitForTat(PlayerParent):
def rule(self, round_num):
if len(self.opponent_moves_history) == 0:
return True
else:
return self.opponent_moves_history[round_num - 1]
class Random(PlayerParent):
def rule(self, round_num):
random_num = random.uniform(0, 1)
if random_num >= 0.5:
return True
else:
return False
Random = Random()
Random.id = 1
TitForTat = TitForTat()
TitForTat.id = 2
def match(a, b):
game_counter = 1
# while game_counter <= 10:
#a_result = a.rule(game_counter)
# b_result = b.rule(game_counter)
# print(a_result, b_result)
# a.append_self_history(game_counter, a_result)
# b.append_opponent_history(game_counter, a_result)
# b.append_self_history(game_counter, b_result)
# a.append_opponent_history(game_counter, b_result)
# a.score_keeper(game_counter)
# b.score_keeper(game_counter)
# game_counter += 1
# print(a.get_score(), b.get_score())
a.self_moves_history[1] = True
print(a.self_moves_history, '\n', b.self_moves_history)
match(Random, TitForTat)
即使没有对 b 类变量进行任何更改,生成的 a.self_moves_history 和 b.self_moves_history 也是相同的。
我注释掉了代码块只是为了测试哪里出了问题。
解决方案
您正在创建opponent_moves_history
一个类变量,因此自然对它的任何更改都是类范围的。
在您的情况下,您应该改为 make opponent_moves_history
,以及self_moves_history
和id
实例变量,以便对它们所做的更改特定于实例。
class PlayerParent():
def __init__(self):
self.id = 1
self.opponent_moves_history = {}
self.self_moves_history = {}
推荐阅读
- python - 如何提取表格中的特定年份范围?
- netcdf - 对不包含时间维度的文件使用 xarray open_mfdataset
- sql - 如何将 Bigquery 中的 Cross Join 和 String Agg 与日期时间差结合起来
- python - 将自由文本用户回复转换为日期时间
- java - 单击按钮时未加载新的 FXML 场景
- wpf - 如何签署窗体应用程序,因为它不是我的选择
- c++ - 获取结构成员的指针后无法正确访问
- javascript - 如何从 fetch REACT.js 中显示地图功能
- android - 如何使用 QT 在 Android 设备上显示图像?
- prolog - 最新的 Prolog 实现基准?