Python basic 6-(high order, anonymous, partial) functions | Decorative Device

Source: Internet
Author: User

It's a bit more advanced here, and it's a little interesting to learn here, but I've seen the similarities with other languages.

higher-order functions are called higher-order functions by passing other functions as parameters.defAdd (x, Y, f):returnF (x) +f (Y) Add (-5, 6, ABS)# One#anonymous function Python uses lambda to create anonymous functionssum =LambdaArg1, Arg2:arg1 +Arg2sum (10, 20)# -#The reduce built-in function is a two-element operation function that uses a data collection of all data for two-dollar operation#a func () operation is performed on the 1th, 2 data of the set, and the resulting result is run with the third data in Func (), so that a result is finally obtained.#as the name implies, reduce shrinks a list to a value fromFunctoolsImportReducel= [1,2,3,4,5]Print(Reduce (LambdaX, Y:x-y, 1))#At the beginning of x, assign a value of 10, and then clickPrint(Reduce (LambdaX, Y:x-y, L, 10))#map applies to each iteration of the item returns a result the List,map function iterates through each parameter with the corresponding processing function#The essence is to change the original list into another list according to the Lambda Law.L = [1, 2, 3]new_list= List (Map (LambdaI:i+1, L))#became [2, 3, 4]L2= [4, 5, 6]new_list= List (Map (LambdaX, Y:x +y, L, L2)) #became [5, 7, 9]#filter processing of sequencesL = [100, 20, 24, 50, 110]new= List (Filter (LambdaX:x < 50, L))#[A]#@before @test in decorators and test methods @end similar to the parameters and multiple adorners#in short, you need to do something uniformly when you're dealing with a method. fromFunctoolsImportWrapsdefMakehtmltag (tag, *args, * *Kwds):defReal_decorator (FN):#fn is hello ()Css_class ="class= ' {0} '". Format (kwds["Css_class"])             if "Css_class" inchKwdsElse ""            defWrapped (*args, * *Kwds):return "<"+ tag + Css_class +">"+ FN (*args, **kwds) +"</"+tag+">"            returnWrappedreturnReal_decorator@makehtmltag (Tag="b", css_class="Bold_css") @makeHtmlTag (tag="I", css_class="Italic_css")defHello ():return "Hello World" Print(Hello ())#<b class= ' bold_css ' ><i class= ' italic_css ' >hello world</i>#here are two layers of B for the outermost layer, I for the middle tier#High efficiency recursion There's a problem here is that input 60 will exceed the integer range to give an error . fromFunctoolsImportWraps fromDatetimeImportdatetimedefMemo (FN): Cache={} Miss=object () @wraps (FN)defWrapper (*args): Result=cache.get (args, Miss)ifResult isMiss:result= FN (*args) Cache[args]=resultreturnresultreturnWrapper@memodeffib (n):ifN<2:            returnNreturnFIB (n-1) + fib (n-2) Start=DateTime.Now ()Print(FIB (40))#102334155End =DateTime.Now ()Print((End-start). microseconds)#71061deffib2 (n):ifN<2:            returnNreturnFIB2 (n-1) + FIB2 (n-2) Start1=DateTime.Now ()Print(FIB2 (40))#102334155End1 =DateTime.Now ()Print((END1-START1). microseconds)#641741      #Partial function only sets part of the parameterint'123455')#default conversion to 10 binaryint'123456', base 8)#8 binaryint'123456', base 16)#16 binary#for convenience, you don't need to enter as many binary values each time.defInt2 (x, base=2):    returnInt (x, base)#using functools.partial to create a biased functionImportFunctoolsint2= functools.partial (int, base=2)Print(Int2 ('1000000'))#Pass dictionary can default parametersKW = {'Base': 2}Print(Int ('10010', **kw))#Pass Listargs = (10, 5, 6, 7)Print(Max (*args))

Python basic 6-(high order, anonymous, partial) functions | Decorative Device

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.