Python path-function basics and Parameters

Source: Internet
Author: User

I. Function definition
def function name (ARG1,ARG2):        =============> function definition     ' description information '                 ===== ========>Print(function name.  __doc__)   Show comment    function body                      =============> function Body     return 1                  =============> The return value can be any type

1. Null function

def function name ():

Pass
Must add pass, generally in function function definition, convenient later to write

 

2. Built-in functions

Built into the Python interpreter, the builtins can be called directly


3. Return value related

A. Do not write return will actually return none

B.return multiple values    returns a tuple

C. function can only execute once return

4. Interrupt a variable decompression:
a.  a,b,c,d = [1,2,3,4]      ===> a=1 b=2 c=3 d=4

b.  head,*_=[1,2,3,4]      ===>head=1

C. def fun ():
    return The


A,b,c = Fun () ===> a=1 b=2 c=3

Two. Function parameters

The variable name in parentheses in the function definition is a formal parameter; the arguments passed in parentheses when the function is called are actual parameters, and the actual number of arguments passed in must be the same as the formal parameters.

It is not recommended to pass a variable type parameter as the actual parameter of the function, as far as possible the function of the function is only related to itself, do not change the contents of the same level of global variables.

    • Actual parameters: (call simultaneous value)

1. Pass the value by position F (
2. Pass the value by keyword F (x=1,y=2)
3. Mixed use, by location value must be passed by the keyword before the value f (1,y=2)

    • Formal parameters: (defined when defined)   

1. Every form parameter must be passed in.
2. The default parameter def fun (x,y=1) position parameter must precede the default parameter;

The usual parameters are set to the default parameters, the call can not be passed in the value, but can still be passed in the value of
such as fun (5) or fun (5,5)

    • Dynamic Parameters:

1.*args drop-by-position value, you can receive multiple parameters to a tuple

formal parameter side use
def foo (*args):
print (args)
Foo (===>) args = (All-in-all)

used by the actual participants
def bar (x, y, z)
print (x, y, z) ===>1 2 3
Bar (* (+) )
that is, *args actually corresponds to multiple positional parameters, and args is a tuple of multiple positional parameter combinations.


2.**kwargs Remove the value passed by the keyword, the other parameters received into a dictionary

formal parameter side use
def foo (x=2,**kwargs):
print (x)
print (Kwargs)
foo (x=1,y=2,z=3) ===> x = 1 Kwargs = {' Y ': 2, ' Z ': 3}

used by the actual participants
def bar (x, Y, z):
print (x, y, z)
Bar (**{' x ': 1, ' Y ': 2, ' Z ': 3}) ===> x = 1 y = 2 Z = 3

    That is, **kwargs actually corresponds to more than one keyword argument, and Kwargs is a dictionary of multiple keyword combinations.


3. Mixed use
def foo (*args,**kwargs):
Print (args)
Print (Kwargs)
Foo (1,2,3,a=5,b=6,c=7) ===>args = (Kwargs) = {' A ': 5, ' B ': 6, ' C ': 7}

(*args,**kwargs) can actually receive any number of positional parameters and any number of keyword parameters, increasing the flexibility of function parameters

Python path-function basics and Parameters

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.