You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确践行OOP与MVC?修改嵌套对象属性的规范方法

国际象棋赛事结果录入代码优化建议

我正在通过实践学习OOP与MVC,编写了一段国际象棋赛事录入比赛结果的Python代码,但感觉代码不够优雅,想了解修改嵌套对象属性的恰当、简洁方式。核心代码如下:

def enter_match_results(self, nth_round, nth_match):
        n_round = self.tournament.get_list_of_rounds()[nth_round]
        game = n_round.get_list_of_match()[nth_match]
        players = game.get_players()

        for i, player in enumerate(players):
            if i == 0:
                valid_input = 0
                while not valid_input:
                    score = self.view.ask_results(player)
                    if score == "0" or score == "0,5" or score == "0.5" or score == "1":
                        if ',' in score:
                            score = score.replace(',', '.')
                        valid_input = 1
                        self.tournament.get_list_of_rounds()[nth_round].get_list_of_match()[nth_match].set_score_player_1(float(score))
                        
                    else:
                        self.view.display_score_error()
            elif i == 1:
                valid_input = 0
                while not valid_input:
                    score = self.view.ask_results(player)
                    if score == "0" or score == "0,5" or score == "0.5" or score == "1":
                        if ',' in score:
                            score = score.replace(',', '.')
                        valid_input = 1
                        self.tournament.get_list_of_rounds()[nth_round].get_list_of_match()[nth_match].set_score_player_2(float(score))
                        
                    else:
                        self.view.display_score_error()

优化建议:

  • 提取重复的输入验证逻辑
    将分数输入、验证、格式转换的重复代码封装成独立方法,避免冗余,提升可维护性。比如新增一个_get_valid_score方法:

    def _get_valid_score(self, player):
        VALID_SCORES = {"0", "0.5", "0,5", "1"}
        while True:
            score = self.view.ask_results(player)
            if score in VALID_SCORES:
                return float(score.replace(',', '.'))
            self.view.display_score_error()
    
  • 简化嵌套对象访问
    代码中已提前获取game对象,无需重复通过self.tournament.get_list_of_rounds()[nth_round].get_list_of_match()[nth_match]访问,直接用game对象调用set方法即可,减少嵌套层级与重复计算。

  • 优化分数合法性判断
    用集合存储合法分数字符串,判断时直接用score in 集合,比多个or拼接更简洁高效。

  • 统一分数格式处理
    无需单独判断是否包含逗号,直接调用replace(',', '.'),即使没有逗号也不会影响结果,简化逻辑。

优化后的代码:

def enter_match_results(self, nth_round, nth_match):
    n_round = self.tournament.get_list_of_rounds()[nth_round]
    game = n_round.get_list_of_match()[nth_match]
    players = game.get_players()

    for i, player in enumerate(players):
        valid_score = self._get_valid_score(player)
        if i == 0:
            game.set_score_player_1(valid_score)
        elif i == 1:
            game.set_score_player_2(valid_score)

def _get_valid_score(self, player):
    VALID_SCORES = {"0", "0.5", "0,5", "1"}
    while True:
        score = self.view.ask_results(player)
        if score in VALID_SCORES:
            return float(score.replace(',', '.'))
        self.view.display_score_error()

内容的提问来源于stack exchange,提问作者Yann Bronner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:01:12