Partial function usage in Python

Source: Internet
Author: User

Partial Application of function

When the function is executed, it is invoked with all the necessary arguments. However, sometimes parameters can be known in advance of a function before it is invoked. In this case, one or more parameters of a function can be used beforehand so that the function can be invoked with fewer arguments.

Example

The code is as follows Copy Code

>> from operator import Add, Mul
>>> from Functools Import partial
>>> add1 = Partial (add, 1)
# add1 (x) = = Add (1, x)
>>> mul100 = partial (mul) # mul100 (x) = = Mul (MB, x)
>>>
>>> Add1 (10)
11
>>> add1 (1)
2
>>> mul100 (10)
1000
>>> mul100 (500)

Sometimes this kind of operation allows us to show our programs more clearly.

For example:

The code is as follows Copy Code

In [9]: from functools import partial

In [ten]: def add (a,b):
....: Return A+b
....:

In [one]: Add (4,3)
OUT[11]: 7

in [[]: plus = partial (add,100)

in [+]: Plus (9)
OUT[13]: 109

In []: Plus2 = partial (add,99)

in [[]: Plus2 (9)
OUT[15]: 108

In fact, when the function is called, there are multiple parameter parameters, but one of the parameters is already known, we can rebind a new function through this parameter, and then call the new function.

If there are default parameters, they can also automatically correspond to, for example:

The code is as follows Copy Code

in [[]: Def add2 (a,b,c=2):
....: Return A+b+c
....:

in [[]: Plus3 = Partail (add,101)
---------------------------------------------------------------------------
Nameerror Traceback (most recent call last)
/users/yupeng/documents/phantomjs/<ipython-input-18-d4b7c6a6855d> in <module> ()
----> 1 PLUS3 = Partail (add,101)

Nameerror:name ' Partail ' is not defined

in [[]: Plus3 = partial (add,101)

In [m]: PLUS3 (1)
OUT[20]: 102

in [[]: Plus3 = partial (add2,101)

in [[]: Plus3 = partial (add2,101) (1)
OUT[22]: 104

in [[]: Plus3 (1)
OUT[23]: 104

in [[]: Plus3 (1,2)
OUT[24]: 104

in [[]: Plus3 (1,3)
OUT[25]: 105

in [+]: Plus3 (1,30)
OUT[26]: 132

Partial Change method Default parameter

#!/usr/bin/env python
#-*-Coding:utf-8-*-
#python2.7x
#partial. py
#authror: Orangleliu

'''
Functools partial can be used to change a method default parameter
1 Change the default value of the original default value parameter
2 Add a default value to an argument that has no default value
'''
def foo (a,b=0):
'''
int add '
'''
Print A + b

#user default Argument
Foo (1)

#change default Argument once
Foo (1,1)

#change function ' s default argument, and can use the function with new argument
Import Functools

foo1 = Functools.partial (foo, b=5) #change "B" default argument
Foo1 (1)

Foo2 = Functools.partial (foo, a=10) #give "a" default argument
Foo2 ()

'''
Foo2 is a partial object,it only has three read-only
I'll list them
'''
Print Foo2.func
Print Foo2.args
Print Foo2.keywords
Print dir (foo2)

# #默认情况下partial对象是没有 __name__ __doc__ property, using Update_wrapper to add attributes from the original method to the partial object
Print foo2.__doc__
'''
Execution results:
Partial (func, *args, **keywords)-new function with partial application
Of the given arguments and keywords.
'''

Functools.update_wrapper (Foo2, foo)
Print foo2.__doc__
'''
Modify the Document information for Foo
'''

This way, if we use a method that always requires a few default parameters, do a package and then do not have to set the same parameters each time.

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.