英文:
Combining two functions in one
问题
import numpy as np
class Board:
def __init__(self):
self.cells = np.zeros((3, 3, 3, 3))
self.block = np.zeros((3, 3))
def mark_combined(self, main_row, main_col, row=None, col=None, player=None):
if row is None and col is None and player is None:
self.block[main_row][main_col] = player
else:
self.cells[main_row][main_col][row][col] = player
英文:
How can I combine functions mark_block and mark_cell into single function?
import numpy as np
class Board:
def __init__(self):
self.cells = np.zeros((3, 3, 3, 3))
self.block = np.zeros((3, 3))
def mark_block(self, main_row, main_col, player):
self.block[main_row][main_col] = player
def mark_cell(self, main_row, main_col, row, col, player):
self.cells[main_row][main_col][row][col] = player
EDIT: Would it also be possible to do it without if statement?
答案1
得分: 1
import numpy as np
class Board:
def __init__(self):
self.cells = np.zeros((3, 3, 3, 3))
self.block = np.zeros((3, 3))
def mark(self, main_row, main_col, player, row=None, col=None):
if row is None and col is None:
self.block[main_row][main_col] = player
else:
self.cells[main_row][main_col][row][col] = player
英文:
import numpy as np
class Board:
def __init__(self):
self.cells = np.zeros((3, 3, 3, 3))
self.block = np.zeros((3, 3))
def mark(self, main_row, main_col, player, row=None, col=None):
if row is None and col is None:
self.block[main_row][main_col] = player
else:
self.cells[main_row][main_col][row][col] = player
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论