Skip to main content
 首页 » 编程设计

python之具有一定数量可能结果的单个函数是否应该在 Python 中分解为单独的函数

2024年10月25日13findumars

<分区>

例如,下面的函数移动 玩家根据方向给定数量 通过:

def move(player, direction, amount): 
    if direction == 'up': 
        player.y -= amount 
    elif direction == 'down': 
        player.y += amount 
    elif direction == 'left': 
        player.x -= amount 
    elif direction == 'right': 
        player.x += amount 

不是将方向作为字符串传递,而是应该 该功能可以分解为四个更小的功能吗?

def move_up(player, amount): 
    player.y -= amount 
 
def move_down(player, amount): 
    player.y += amount 
 
def move_left(player, amount): 
    player.x -= amount 
 
def move_right(player, amount): 
    player.x += amount