Learning Log---Python (functional programming)

Source: Internet
Author: User

Function-Type programming

The following closures and adorners are important , parameters can be functions, and functions are passed in to participate in the operation.

A procedure is a function that has no return value, and the function itself is an object, so it can be assigned to a reference;

A function in a module is not a duplicate, so overloading is not supported because Python identifies which of the calls with the function name;

The definition of a function in a module is top-down;

Can be multiple return values, as a tuple, or you can separate multiple variables to accept the return value;

Def p (): Return 1,2,3a=p () print aa,b,c=p () print A,b,c

Properties of the function

Document properties: __doc__

You can pass things directly into the function;

function parameters

Positional parameters, which must be passed in; the default parameter, which can be passed in, is used when defining the function =: Enter default values, use default values when not in the world, variable length parameters, variable parameters of *args tuple type, **kwargs variable parameters of dictionary type;

These different parameter types can be used either individually or together.

# positional parameters, default parameters, variable parameters (tuple, dictionary) def func (Name,count=1,*args,**kwargs): Print name print count print args print kw   args# if passed in to confirm the name value, as with the function, then the count=1,name=2 func ("Python", 12,13,14,15,code=12) # corresponds output to Python 12 (13, 14, 15) {' Code ': ' 12} func ("Python", * (12,13,14,15), **{' Code ' =12}) #这里后面的可变参数会有一部分当作默认参数传入, which is the default pass-through, followed by a mutable parameter

The parameters of the function will be used only if the preceding parameters are met, and the positional parameters must be passed in.


Lambda

itself is also a function, is an anonymous function, can only be a row

#表示传入x, return x+1a = Lambda X:x+1print A (1)

Scope of the variable

x = 1def func (): #这里的global作用是声明这个x是外部的x Global x x+=1 print Xfunc ()

Closure of the function:

def f1 (x): def f2 (y): return x*y return f2# Here the value of the function F1 passed in, to F2, equivalent to the operation of F2 before the injection of a pre-value, input xf2 = F1 (Ten) #输入了yprint F2 (10)

Decorative Device


@start (' Shizhen ')

This is the adorner, used on top of the function.

Function call can be a layer, outside the parameters, the inner layer can be used;

def start (name): Def wrapper (f): Def wrapper1 (): print "Start by" +name F () return Wrapper1 return wrapper# The adorner to be placed above the function @start (' Shizhen ') def f (): print "function" #这里运行时运行 @ the start, will put f this function in the second layer when the Line, the characteristic is that the parameter can be a function, no longer just a separate object, but a function. The first layer of start is passed directly to the F function if there is no parameter ' Shizhen '. F ()


Exercises:

1. Implement a function Max, accept the arguments of any integer, return the maximum value

def Max (*args): length = Len (args) if length = = 0:print "Empty" Else:result = Args[0] For I in range (0,length): If result < Args[i]: result = args[i] print "Max is" + STR (re Sult) Max (1,2,3,5,2,3,7,8,2,1,10)

2. Implement a parameter adorner @callfunction (caller name), the passed parameter is the name of the caller, before calling the function, print a "the caller is [caller name]" of the log

def callfunction (name): Def wrapper (f): #这一层的作用是传入f的传入参数, here is empty, available *args (variable parameter) as the Universal parameter Def wrapper1 ():     Print "The caller is" + Name F () return Wrapper1 return wrapper@callfunction ("Shizhen") def f (): Print "Shizhen is so Cool" f ()

The adorner requires a layer of first parameters and functions are all passed in, the internal self-built layer in order to put forward all the operations.

CallFunction (' Shizhen ') (f) ()


3. Implement an adorner @timeit, the role of this adorner is when the function is finished, print out the specific time of use

#第一层传入func函数def Timeit (f): #第二层传入func函数的输入参数 def wrapper (*args,**kwargs): Print (Time.strftime ("%h:%m:%s")) F (*args,**kwargs) print (Time.strftime ("%h:%m:%s")) return wrapper@timeitdef func (*args): For I in rang E (0,len (args)): if args[i] > 10:print "find:" + str (args[i]) time.sleep (1) func (1,23,12,31 , 2,1,23,12,31,23,1,23,1,23,4,3,42,3,42,34,111)

In the functional programming, the input parameters of the parameters are separated, a whole is passed, the function ontology is transmitted, and then the inputs of the function are transferred.

Timeit (f) (*AGRS)

*agrs can be more than one parameter, or it can be no parameter.

*args,**kwargs. It is very safe to accept parameters with these two functions as parameters.

That is, a layer of a layer of things into the, adorner parameters + function + function parameters (*args,**kwargs This is very safe).

Learning Log---Python (functional programming)

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.