如何在不更改父类变量本身的情况下更改子类的父类中定义的变量?

时间:2018-07-15 03:03:32

标签: python python-3.x class

我试图基于同一个父类创建两个子类,以使它们每个都具有在父对象中定义的相同变量的自己的版本。但是我意识到,在这些子类之一中更改这些变量将导致另一个子类中的版本也发生更改。我知道我可能不太了解继承的概念。请帮忙!

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也相同。

我注释掉了部分代码,只是为了测试哪里出错了。

1 个答案:

答案 0 :(得分:1)

您将opponent_moves_history设为类变量,因此对它的任何更改自然都在整个类范围内。

在您的情况下,您应该使用opponent_moves_history以及self_moves_historyid实例变量,以便对它们进行的更改特定于实例。

class PlayerParent():
    def __init__(self):
        self.id = 1
        self.opponent_moves_history = {}
        self.self_moves_history = {}