多语言展示
当前在线:1230今日阅读:75今日分享:44

如何运用PYTHON里*args和*kwargs

如何运用PYTHON里*args和*kwargs
工具/原料

PYTHON

方法/步骤
1

打开Python,新建一个空白的PY文档。

2

def new_students(*args):    for all_students in args:        print(all_students)the_list = ['Peter'驾王闲, 'Cherry', 'Ben', 'Ken', 'Lee']new_students(*the_list)首先我们用for loops来把list里面的所有值给打印出来。海互

3

def new_students(*args):    for all_students in args:        print(all_students)the_list = ['Peter', 'Cherry', 'Ben', 'Ken', 'Lee']new_students(the_list)如果不加*,那么只会整个列表呈现出来,而不酱披是只把值给返回来。

5

def new_student(classroom, year, *args):    print(classroom)    print(year)    for all in args:        print(all)the_list = ['Peter', 'Cherry', 'Ben', 'Ken', 'Lee']new_student('001', '2018', *the_list)增加两个变量在前面也不影响使用。

6

def new_students(*args):    for all_students in args:        print(all_students)the_list = ('Peter', 'Cherry', 'Ben', 'Ken', 'Lee')new_students(*the_list)除了list,tuples也是可以运用得上。

7

def details(**kwargs):    for key, value in kwargs.items():        print(key)        print(value)contact = {'Peter':'18', 'Alice':'16', 'Ben':'17'}details(**contact)**kwargs对应的要用dictionary。

注意事项

注意*一定要加上

推荐信息