人话Python:__new__究竟是个啥?

本文以浅显易懂的方式解释Python中的__new__方法和super()函数。通过实例和MRO列表,揭示了super()函数在调用父类方法时的顺序,并探讨了__new__方法的执行过程和重要特性。
摘要由CSDN通过智能技术生成

本文主要是自己在学习python中的一点心得,用自己这个阶段的小白能理解的“人话”,把一些不是很复杂但确实难理解的知识点讲清楚、讲明白。

文中很多地方都用了简写、不规则命名方式,主要是想以弄懂知识点为主,大家不要学我,还是按规则敲代码。


要想理解__new__的用法,首先要搞明白 super() 函数的底层实现逻辑,至于为什么后文讲通了就明白了

super()函数

一、常用场景

在类的继承中,如果子类重定了父类的方法,就会覆盖父类的同名方法,想要再调用父类的这个同名方法有两种办法:

1.使用未绑定方法

        父类名.同名方法名(参数)

2.使用super()函数

        2.0写法:super(子类名,self).父类方法名(父类方法参数)

        3.0写法:super().父类方法名(父类方法参数)

最常用到super()函数的情况就是在子类中调用父类的构造方法

因为子类要创建自己的构造方法,就一定要在自己的构造方法中调用父类的构造方法,差不多这么写:

class ZiLei(FuLei):
    def __init__(self):
    super(ZiLei,self).__init__(a,b)

二、深入研究

假设现在有4个类:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是五子棋的Python代码,使用了博弈树搜索算法。 ```python import copy # 棋盘大小 BOARD_SIZE = 15 # 空白点 BLANK = 0 # 黑子 BLACK = 1 # 白子 WHITE = 2 class Board: def __init__(self): # 初始化棋盘 self.board = [[BLANK] * BOARD_SIZE for _ in range(BOARD_SIZE)] # 初始化当前下棋方 self.current_player = BLACK def get_current_player(self): return self.current_player def set_current_player(self, player): self.current_player = player def get_board(self): return self.board def set_board(self, board): self.board = board def get_blank_positions(self): # 获取空白点位置 positions = [] for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): if self.board[i][j] == BLANK: positions.append((i, j)) return positions def is_valid_position(self, position): # 判断位置是否在棋盘内 return 0 <= position[0] < BOARD_SIZE and 0 <= position[1] < BOARD_SIZE def is_blank_position(self, position): # 判断位置是否为空白点 return self.board[position[0]][position[1]] == BLANK def is_win(self, player): # 判断是否获胜 for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): if self.board[i][j] == player: # 水平方向 if j <= BOARD_SIZE - 5 and self.board[i][j:j+5] == [player] * 5: return True # 垂直方向 if i <= BOARD_SIZE - 5 and [self.board[k][j] for k in range(i, i+5)] == [player] * 5: return True # 正斜方向 if i <= BOARD_SIZE - 5 and j <= BOARD_SIZE - 5 and [self.board[i+k][j+k] for k in range(5)] == [player] * 5: return True # 反斜方向 if i >= 4 and j <= BOARD_SIZE - 5 and [self.board[i-k][j+k] for k in range(5)] == [player] * 5: return True return False class Game: def __init__(self): self.board = Board() def get_available_positions(self): # 获取可下棋位置 return self.board.get_blank_positions() def play(self, position): # 下棋 if self.board.is_valid_position(position) and self.board.is_blank_position(position): self.board.get_board()[position[0]][position[1]] = self.board.get_current_player() return True return False def switch_player(self): # 切换下棋方 current_player = self.board.get_current_player() if current_player == BLACK: self.board.set_current_player(WHITE) else: self.board.set_current_player(BLACK) def is_win(self): # 判断是否获胜 return self.board.is_win(BLACK) or self.board.is_win(WHITE) def is_draw(self): # 判断是否平局 return len(self.get_available_positions()) == 0 class AI: def __init__(self, game): self.game = game def get_move(self): # 博弈树搜索算法 best_score = -float('inf') best_move = None for position in self.game.get_available_positions(): new_game = copy.deepcopy(self.game) new_game.play(position) score = self.minimax(new_game, 2, False) if score > best_score: best_score = score best_move = position return best_move def minimax(self, game, depth, is_maximizing): # 极小化极大算法 if depth == 0 or game.is_win() or game.is_draw(): return self.evaluate(game) if is_maximizing: best_score = -float('inf') for position in game.get_available_positions(): new_game = copy.deepcopy(game) new_game.play(position) score = self.minimax(new_game, depth - 1, False) best_score = max(best_score, score) return best_score else: best_score = float('inf') for position in game.get_available_positions(): new_game = copy.deepcopy(game) new_game.play(position) score = self.minimax(new_game, depth - 1, True) best_score = min(best_score, score) return best_score def evaluate(self, game): # 评估函数 if game.is_win(): if game.board.get_current_player() == BLACK: return 1 else: return -1 else: return 0 def main(): game = Game() ai = AI(game) while True: # 人类玩家下棋 print(game.board.get_board()) human_position = tuple(map(int, input("Your move (row, col): ").split(','))) while not game.play(human_position): print("Invalid move!") human_position = tuple(map(int, input("Your move (row, col): ").split(','))) if game.is_win(): print(game.board.get_board()) print("You win!") break if game.is_draw(): print(game.board.get_board()) print("Draw!") break # AI下棋 ai_position = ai.get_move() print("AI's move:", ai_position) game.play(ai_position) if game.is_win(): print(game.board.get_board()) print("AI wins!") break if game.is_draw(): print(game.board.get_board()) print("Draw!") break if __name__ == '__main__': main() ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布朗思东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值