Functions of Python Basics

Source: Internet
Author: User

A Python def statement is actually an executable statement: when it runs, it creates a new function object and assigns it to a variable name. Because it is a statement, a def can appear where any of the statements can now occur.
1 if Test: 2     def func (): 3         Pass 4 Else : 5     def func (): 6         Pass
View Code

The return statement for Python can appear anywhere within the body of the function. It represents the end of the function call and returns the result to the function call. The return statement is optional, and if it does not appear, then the function will end when the control flow finishes executing the function body. A function that has no return value automatically returns the Non object. The   namespace, also known as the scope     Inline module, is the global scope. Each module is a global scope     Global scope is scoped to a single file     each call to a function creates a new local scope     assigned variable name unless declared as a global variable or a non-local variable. Otherwise all local variables     All other variable names can be summed up as local, global, or built-in   variable name resolution: LEGB principle     local scope     Local scope of def or lambda in the superstructure (when nested functions in functions are considered)     global scope     built-in scope  nonlocal, Allows assigning values to names in nested function scopes (local scopes can assign values to variables in the upper scope)   function parameter matching model     location: match from left to right         Keyword parameters: match     default parameters by keyword: Define parameter values for parameters that do not have an incoming value     mutable parameters: Collect any number of parameters based on location or keyword     Variable parameter unpacking: Pass any number of parameters based on location or keyword     keyword-only parameters: parameters must be passed by name   in function calls, parameters must appear in this order: any positional parameter (value), followed by the keyword argument (name=value) and the *sequence combination, and then the **dict on the function head, the arguments must appear in this order: any general parameter (name), default parameter (name=value), if any, followed by *name ( Python3 *), followed by any name or name=value keyword-only parameter (python3), followed by **name    *args to collect the mismatched positional parameters, put it in a tuple, and assign the tuple to the variable A.rgs    **args only valid for keywords (name=value), pass these keyword arguments to a new dictionary and be able to use keys or iterations.  
1 #!/usr/bin/env Python32 3 deffunc (A, B, c):4     Print(A, B, c)5Func (c=3, b=2, a=1)#keyword Parameters6Func (1, c=3, b=2)#keyword Parameters7 8 defFunc1 (A, b=2, c=3):9     Print(A, B, c)Ten  OneFUNC1 (1)#default parameters, and if you do not assign a value to B and C, the default value is assigned.  AFunc1 (1, 4)#If you pass two parameters, only C gets the default value.  -  - #Position Parameters the defF (*args): -     Print(args) - " " - When this function is called, Python collects all positional-related parameters into a new tuple and + assign this tuple to args because it is a tuple object that can be indexed or for loop - to step in.  + " " AF (1, 2) at  - #The xx attribute is similar, but only through the key word pass parameter. Pass these keyword arguments to a new dictionary.  - #You can then access the dictionary by key.  - defF1 (* *args): -     Print(args['a'], args['b']) -  inF1 (A=1, b=2) -  to #Unpacking Parameters + defF2 (A, B, C, D): -     Print(A, B, C, D) the #we can pass four arguments (x statements) to a function through a tuple, which will unpack the parameters.  *args = (1, 2, 3, 4) $F2 (*args)Panax Notoginseng  - #Similarly, * * The dictionary is unpacked as a key/value pair, making it a keyword parameter the  +args= {'a': 1,'b': 2,'C': 3,'D': 4} AF2 (* *args) the  + #In addition, in the process of calling a function, you can mix common parameters in a flexible way.  -F2 (* (), **{'D': 4,'C': 3}) $F2 (1,* (2,), **{'D': 4,'C': 3})
View CodeKeywords-only:def f (a,*,c), * the following parameters only receive the keyword parameters, Def f (a,*b,c), a may receive parameters by location or name, B receives any additional parameters, C can only receive parameters via the keyword
" " The keyworld-only parameter with the default value is optional, but those keyworld-only parameters that do not have a default value really become the keyworld-only parameter that the function must have " " def F3 (A, *b, C, d='spam'):    print(A, B, C, D) F3 ( 1,2,3,4,c=5)  #  ---> 1 (2, 3, 4) 5 spam #--  error
View Codeanonymous function lambda    general form: The keyword lambda after one or more parameters, followed by a colon, followed by an expression.           LAMBDA is an expression, not a statement           The subject of  LAMBDA is a single expression, not a code block     Map is one of the places that lambda usually appears     filter filters Some elements according to the function, applying the function to each element in the iterator     Reduce applies a function to each pair of elements and runs to the final result, which is not an iterator, it returns a single result    filters some elements based on a function: filter applies a function to each pair of elements and runs to the final result: reduce      print (Reduce (lambda x,y:x-y), [10,9,6]): Each step, reduce passes the current difference and the next element, which is passed to the lambda.   Since the function name is a reference to a function object, we can assign an empty object to another name and call it
def Echo (msg):     Print  = Echof ('Hello world! ')   # --->hello world
View Code

We can also view the properties of a function by Dict (func), or add a new property to the function
def Echo (msg):     Print  = Echof ('Hello world! ')   # --->helloworld echo.handles="button"echo.handles   #  ---> button
View Code

Functions of Python Basics

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.