Python定義函數

來源:互聯網
上載者:User

標籤:調用   div   ted   val   lis   tip   after   syntax   most   

  1. 其他形式1:

    1、定義函數

     

    def test4(a = ()):

        print(‘################test4################‘)

        print(type(a))

        print(a)

    2、調用函數

     

    正確調用:

    test4((1, 2))        #a在函數體內部為tuple類型

    test4(a=(1, 2))    #a在函數體內部為tuple類型

    test4((1,))          #a在函數體內部為tuple類型

    test4(a=(1,))      #a在函數體內部為tuple類型

    test4((1))           #a在函數體內部為int類型,非tuple類型

    test4(a=(1))       #a在函數體內部為int類型,非tuple類型

    test4(1)             #a在函數體內部為int類型,非tuple類型

    test4(a=1)         #a在函數體內部為int類型,非tuple類型

    錯誤調用:

    test4(1, 2)          #TypeError: test4() takes at most 1 argument (2 given)

    test4(1, b=2)      #TypeError: test4() got an unexpected keyword argument ‘b‘

    test4(a=1, b=2)  #TypeError: test4() got an unexpected keyword argument ‘b‘

  2. 5

    其他形式2:

     

    1、定義函數

     

    def test5(b = {}):

        print(‘################test5################‘)

        print(type(b))

        print(b)

    2、調用函數

     

    正確調用:

    test5({‘a‘:2})       #b在函數體內部為dict類型

    test5(b={‘a‘:2})

    test5({‘a‘:2,‘b‘:3})#b在函數體內部為dict類型

    test5(b={‘a‘:2,‘b‘:3})

    test5(b=2)           #b在函數體內部為int類型,非dict類型

    錯誤調用:

    test5(a=1, b=2)   #TypeError: test5() got an unexpected keyword argument ‘a‘

    test5(1, 2)           #TypeError: test5() takes at most 1 argument (2 given)

    test5(1, b=2)       #TypeError: test5() got multiple values for keyword argument ‘b‘

  3. 6

    其他形式3:

     

    1、定義函數

     

    def test6(a = (), b = {}):

        print(‘################test6################‘)

        print(type(a))

        print(a)

        print(type(b))

        print(b)

    2、調用函數

     

    正確調用:

    test6(1, 2)

    test6(a=1, b=2)

    test6(a=1, b=2)

    test6((1, 2), {‘c‘:8})

    test6({‘c‘:8})

    test6(b={‘c‘:8})

    test6((1, 2), b=2)

    test6((1, 2), b=2)

    錯誤調用:

    test6(a=1, 2)       #SyntaxError: non-keyword arg after keyword arg

    test6(1, 2, b=2)   #TypeError: test6() got multiple values for keyword argument ‘b‘

Python定義函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.