Python path-functions

Source: Internet
Author: User

function definition:
def def_name (): "" "xxxxxxxx" "" #文档描述 (note) xxxxxxxxxx #语句块 return x #返回值
Procedure and function differences: a procedure is a function return value problem with no return value: Returns none if there is no return value, or if there are multiple return values in the return value of the method, Python returns them as a tuple. (equivalent to returning a value) parameter invocation:
    1. Positional parameters: Actual participation in Parameter one by one corresponds
    2. Keyword parameter: Actual participation parameter Order Independent
    3. Default parameter: The default parameter can be used when the function is called without arguments and the system will automatically execute the program with the default value
    4. Parameter groups: can be used to pass arbitrary parameters (parameter groups must be put back in formal parameters)
        • **kwargs the delivery of an argument:
          1. Direct delivery: func (key1 = Val1,key2 = Val2 ...) >>>key1, Key2 ... Must be a keyword
          2. Func (**{key1:val1,key2:val2 ...}) S
      • *args: Receives n positional parameters in tuples such as: Func (*args)
        • *args the delivery of an argument:
          1. Direct delivery: Func (1,2,3,4 ...)
          2. Func (*[1,2,3,4 ...])
      • **kwargs: Receive n keyword parameters as a dictionary example: Func (**kwargs)
    • Note:
      1. If the positional parameter call and the keyword argument call mash, then the position and parameter one by one correspond
      2. Keyword argument calls must be followed by the position parameter call, otherwise an error will be given
Global variables and local variables:
variables within a function are local variables by default, and modifying a variable with the same name outside of a function does not have any effect, if you want to modify the global variable within a function then declare the variable to be a global variable instead of a local variable within the function: global Val Modifying the variable after the statement is the modified global variable (the global variable must be explicitly defined outside the function rather than implicitly defined in the function >>> error
-prone)
Special cases: In addition to strings, integers are treated as local variables by default inside the function, and the rest of the complex data types (lists, dictionaries, collections, classes) are considered global variables inside the function (that is, complex data types can modify the global values directly within the function) higher order functions: passing a function as an argument to a function The return value contains the function name (which is used in another function), which is the higher order function, for example:
def Add (a,b,j): Return f (a) + f (b) Add (3,-6,abs) >>> result is 9
Anonymous functions: Anonymous functions are defined using the Lambda keyword, noting that anonymous functions can handle only simple logic (which can handle ternary operations), and if the logic is too complex then the system throws an error
func = lambda n:n*nfunc (5) >>>25 The above code is equivalent to: def func (n): Return N*nfunc (5) If there is no end of the variable anonymous function, then you can call the anonymous function (lambda N:n*n) (5)

Python path-functions

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.