这是一个将列表格式化存储的函数
import sys
def 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)
正确
同理两个参数 如果传入变量 也要和 函数定义的同名