欢迎使用CSDN-markdown编辑器

来源:互联网 发布:python 每1s执行一次 编辑:程序博客网 时间:2024/06/16 02:28

这是一个将列表格式化存储的函数

import sysdef print_lol(the_list,indent=False,level=0,fn=sys.stdout):  for each_item in the_list:    if isinstance(each_item,list):      print_lol(each_item,indent,level+1,fn)    else:      if indent:        for tab_stop in range(level):          print("\t",end="",file=fn);      print(each_item,file=fn)

调用的代码如下

        nester.print_lol(other,True,level1=5,fn = out_other)

这个函数中后三个参数都有默认值
调用的时候 如果传入的是变量名的话 一定要和 函数定义中的 变量名相同 不然会报错
TypeError: print_lol() got an unexpected keyword argument ‘level1’

改为

nester.print_lol(other,True,level=5,fn = out_other)

正确

同理两个参数 如果传入变量 也要和 函数定义的同名

原创粉丝点击