Partial function-python Learning

Source: Internet
Author: User

A function can have multiple parameters, and in some cases some parameters are first obtained, some parameters need to be known in the following scenario, Python provides us with the partial function for carrying some parameters to generate a new function.

def Add (a,b,c=2):    print("a is:%s b is%s c is%s"%(a,b,c )    return a+b+cadd_with_a_b=partial (add,2,3)print(add_with_a _b ()#  it ' s 7add_with_a=partial (add,9)print(add_with_a)#  it ' s 21
#################

A is:2 B is 3 C is 2
7
A Is:9 B is ten C is 2
21st

The following is an alternative to using partial to create an adorner that modifies the function's __doc__ to another function.

#Coding:utf-8" "Created on 2014-10-1@author:zkchen" " fromFunctoolsImportPartialparams=("__doc__",)defUpdate_params (target,source,params=PARAMS):#set Target's property mentioned in the params to source.[SetAttr (Target,p,getattr (Source,p,none)) forPinchPARAMS]returnTargetdefupdate_params_wrap (source):returnPartial (update_params,source=source,params=PARAMS)deftest_partial ():defFunca ():" "This is Funca ' s doc" "        Pass@update_params_wrap (Funca)defFUNCB ():" "This is FUNCB ' s doc" "        Pass    Print(FUNCB.__doc__)
#################################

This is Funca ' s doc

Partial function-python Learning

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.