如何运用PYTHON里*args和*kwargs

2025-07-13 11:19:26

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

如何运用PYTHON里*args和*kwargs

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

如何运用PYTHON里*args和*kwargs

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)增加两个变量在前面也不影响使用。

如何运用PYTHON里*args和*kwargs

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。

如何运用PYTHON里*args和*kwargs
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢