python-Primary Knowledge function

Source: Internet
Author: User

Definition of the function:

Functions are well-organized, reusable, code snippets for single, or associated functionality

Role of the function:

function can improve the modularity of the application, and the reuse of the Code

Any incoming parameter and first line statement can optionally use a document string-----for storing function descriptions

Function contents start with a colon, and indent

Basic format:

def wrapper () #函数的定义

Print (xxxxxxx)

Wrapper () #函数的调用

The return value of the function:

Return

Once the return is encountered, the entire function is ended

Return a value:-------> Only need to add a return at the end of return

return 18

Returns multiple values:--------> can return multiple values, any type of value


Parameters of the function:

Formal parameters: Def wrapper (formal parameters)

Arguments: Wrapper (argument)

Stand at the argument angle: Position parameter, key-word-pass-parameter, mixed-pass parameter
Standing parameter angle: Positional parameters must be passed
--------------------------------------------
Positional parameters default parameter dynamic parameters
corresponding Gender (*args,**kwargs)
--------------------------------------------

To pass the parameter:
Dynamic parameter transfer

---------------------------------------------------------------------------
Namespaces and Scopes

Namespaces:
1. Global namespaces
2. Local namespaces
3. Built-in namespaces

Load Order:
Built-in namespaces (load before program runs)
Global Namespaces (Program running: loading from top to bottom)
Local namespace (program run total: Before the call is loaded)
Order of Values:
In local calls: Built-in namespaces, local namespaces, global namespaces
Global invocation: Built-in namespaces, global namespaces

Scope:
Global scope
Local scope

Global: Local Call to Globals
Locals

Nonlocal: References and modifications to the variables of the parent scope, and to which layer it is referenced, all occurrences of the variable from that level and below

Name of function:
The function name essence is the memory address of the function


Closures:
intrinsic function references to external scope variables, called closures (non-global scopes)

Intrinsic functions:
functions defined inside a function are called intrinsic functions

def func ():
name = ' Taibai Venus '
def inner ():
Print (name)

intrinsic function references to external scope variables, closures
name = ' Taibai Venus '
def inner ()
Print (name)
----------------------------------------
Intrinsic functions:
name = ' Taibai Venus '
def inner ()-----------> intrinsic functions defined inside the function
Print (name)

python-Primary Knowledge 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.