Python3 from zero--{initial consciousness: 007~ function}

Source: Internet
Author: User

To write a function that accepts any number of parameters: *, * *

def Test (x, *args, y, * *Kwargs):   ... Pass  >>> Test (1, 2, 3, 4, 5, 5, y=9, aa=99, bb=88,cc=900)>>> Test (1, 2, 3, 4, 5, 5, 9, aa=9 9, bb=88,cc=900) Traceback (most recent  ):"<stdin>"  in <module>'y'

#以 * The parameter of the beginning can only appear as the last positional parameter, and the argument of the * * will only appear as the last parameter; *args can still have other parameters, but only the keyword parameter (keyword_only)

Second, write functions that accept only keyword parameters

def Test (*, X, y): ...   Pass  >>> Test (8, 9) Traceback (most recent)  :"<stdin> "  in <module>2 were given>>> test (x=9, y=8)

The parameters after #星号 * are keyword_only parameters

Third, function annotation

>>>defAdd (X:int, Y:int)int: ...returnX +y ... Add.__annotations__{'x': <class 'int','return': <class 'int','y': <class 'int'}

#函数注解只会保存在函数的__annotations__属性中; Because there is no type declaration in Python, function annotations can be used to prompt

Four, returning multiple values from a function: separating each return value with a comma "," essentially returning a tuple that can be used to return multiple values through the tuple unpacking implementation

def myfun ():   ... return 1, 2, 3>>> x, y, z = myfun ()>>> x1>>> y2& Gt;>> z3>>> a = myfun ()>>> A (1, 2, 3)

V. Define a function with default parameters

The default parameter is only bound once when the function is defined

def sample (a=x): ...   Print  >>> sample ()44>>> x = 88>>> sample ()44

The default value of the default parameter should normally be an immutable object; If you set a Mutable object, you should refer to the following way:

def Test (A, b=None): ...   if  is None:    ... = []...   Pass

Vi. let callable objects with n parameters be called with fewer arguments

That is, to give some parameters pre-bin to a fixed value, equivalent to converting to a function with a default value

def sum (A, B, C, D): ...   return A + B + C +>>> sum (1, 2, 3, 4)import  functools>>> te ST_0 = functools.partial (sum, b=2, c=3, d=4)>>> test_0 (1)10>>> test_0 (+) 109

You can also use lambda functions to implement

Lambda A, b=2, c=3, d=4: sum (A, B, C, D)>>> test_1 (1)10>>> test_1 (100 )109

Python3 from zero--{initial consciousness: 007~ function}

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.