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