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)

 显示

 

 

3.传入带有键值的数

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

  显示:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM