Introduction to the functions of [Python]

Source: Internet
Author: User

function is a program logic to the structure or process of a programming method, detailed instructions please refer to the "Python Learning Manual", can not be explained in detail in time, hope forgive me!.

First, create a function

1.1 DEF statement

def function (args):

"Documentation"

Function_body_suite

1.2 declarations, definitions and parameters

A declaration defines a function name and a parameter (name), and does not define a body code block of functions

Define a function body code block

Parameters mainly include positional parameters, default parameters, non-keyword arguments (*args), keyword arguments (**kargs), and must follow this order when creating functions. A non-keyword parameter (*args), a keyword argument (**kargs) used in the CREATE function, collects parameters, and is used in the call to unpack the parameters. Remember the default mutable parameter trap.

1.3 Forward References

In Python, a function does not have a forward-referenced rule (who first defines no impact, or even a non-sequential nesting), only needs to be defined before the call

def foo ()

Print (' foo ')

Bar ()

def bar ()

Print (' Bar ')

Foo ()

1.4 Properties

Accessing and adding attributes through the period property identifier, the function's properties are related to the function object, and the scope of the function is independent

1.5 inline functions

Create another function inside the function body

1.6 Function Adorner

@decorator (Dec_opt_args)

def function (args):

"Doc"

Body_suite

#for Example

@g

@f

def foo ():

Pass

foo = g (f (foo))

1.7 Partial function

From Functools Import Partial

ADD1 = Partial (add, 1) # ADD1 (x) = Add (1, x)

Basetwo = partial (int,base=2) #将参数base = 2, fixed to the fixed parameter of the INT function.

1.8 Packet Closure function

If, in an intrinsic function, a reference is made to a variable that is in an outer scope (but not a global scope), then the intrinsic function is a closed packet and has memory.

1.9 Recursion

If the function contains a call to itself, the function is a recursive function

def factorial (n):

if n = = 1 or N = = 0:

Return 1

Else

Return (N*factorial (n-1))

1.10 Functional Programming

Lambda [arg1 [, arg2, ...]]:expr

A lambda expression returns a function object that can be called.

1.11 Built-in functions

Map (), filter (), reduce () all create an iterative context, similar to the for

Map (func, Seqs) iterates through each element in the sequence and processes through the Func function, returning a list of all the values

Filter (func, seqs) iterates through each element in the sequence, leaving the function returned as a Boolean true element, adding all the values to a list and returning

Reduce (func, Seqs) takes the first two elements of the sequence, passes in the two-tuple function func to get a single value, and then passes the value with the next element, again passing in the two-tuple function func, and processing the sequence one at a time until the end.

Introduction to the functions of [Python]

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.