Python-defined functions

Source: Internet
Author: User

  1. Other Forms 1:

    1. Defining functions

    Def test4 (a = ()):

    Print (' ############### #test4 ################ ')

    Print (Type (a))

    Print (a)

    2. Calling functions

    Call correctly:

    Test4 ((1, 2)) #a在函数体内部为tuple类型

    Test4 (a= (1, 2)) #a在函数体内部为tuple类型

    Test4 (1,)) #a在函数体内部为tuple类型

    Test4 (a= (1,)) #a在函数体内部为tuple类型

    Test4 ((1)) #a在函数体内部为int类型, non-tuple type

    Test4 (a= (1)) #a在函数体内部为int类型, non-tuple type

    TEST4 (1) #a在函数体内部为int类型, non-tuple type

    Test4 (a=1) #a在函数体内部为int类型, non-tuple type

    Error Call:

    Test4 (1, 2) #TypeError: Test4 () takes at the 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

    Other Forms 2:

     

    1, define functions

     

    Def test5 (b = {}):

        print (' ############### #test5 ################ ')

        Print (type (b))

        print (b)

    2, call function

     

    correct invocation:

    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类型, non-dict type

    Error Invocation:

    Test5 (A=1, b=2)   #TypeError: Test5 () got an unexpected keyword argument ' a '

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

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

  3. 6

    Other Forms 3:

    1. Defining functions

    Def test6 (a = (), B = {}):

    Print (' ############### #test6 ################ ')

    Print (Type (a))

    Print (a)

    Print (type (b))

    Print (b)

    2. Calling functions

    Call correctly:

    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)

    Error Call:

    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-defined functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.