小编Irc*_*ros的帖子

在 __init__ 函数中处理多个参数的优雅方法

我有以下 init 函数,它接收大量参数来运行该类(如果用户不输入任何内容,则参数是默认值,或者可以是用户输入的值)。在不损失可读性的情况下减少变量数量(不在 init 中显示大量参数)的最优雅的方法是什么?使用*args函数(如def__init__(self, *args))?

    class World(object):
    
        def __init__(self, grid_size=(GRID_WIDTH, GRID_HEIGHT),
                     cell_size=(CELL_WIDTH, CELL_HEIGHT),
                     obstacles_position= OBSTACLES,
                     recharge_position= RECHARGE_ZONE,
                     treadmill_position= TREADMILL_ZONE,
                     workers_positions= WORKERS_POS,
                     delivery_positions= DELIVERY_ZONE):
         # some code bellow
    def main():
       # init some libraries
       world = worldGrid()
       # Do a while loop with the input variables from the world class

    if __name__ = '__main__':
       main()
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
1
解决办法
1582
查看次数

标签 统计

python ×1

python-3.x ×1