python 传入任意多个参数(方法调用可传参或不传参)

时间:2023-03-09 23:36:05
python 传入任意多个参数(方法调用可传参或不传参)

1.可传参数与不传参数,在定义中给参数设置默认值

class HandleYmal:
"""
获取测试环境的配置
"""
def __init__(self,file_path=None):
if file_path:
self.file_path=file_path
else:
#获取path
root_dir=os.path.dirname(os.path.abspath('.'))
print(root_dir)
self.file_path=root_dir+"/config/base.yaml"
#调用时没有写参数,实际也可以用参数
if __name__ == '__main__':
test=HandleYmal()
p=test.get_data()

2.传入单个数据

*key,里面就是带单个元祖数据

def show_argg(self,*key):
print(key)
调用:
if __name__ == '__main__':
test=HandleYmal()
  test.show_argg(1,2,3,4)

 显示

 python 传入任意多个参数(方法调用可传参或不传参)

3.传入带有键值的数

**kwargs 传入可以是键值类型
def show_arags(self,name,**kwargs):
print(name)
print(kwargs)
if __name__ == '__main__':
test=HandleYmal()
test.show_arags("jun1",a="jun")

  显示:

python 传入任意多个参数(方法调用可传参或不传参)