Partial functions-python learning, partial functions-python

Source: Internet
Author: User

Partial functions-python learning, partial functions-python

A function can have multiple parameters. In some cases, some parameters are obtained first, and some parameters must be known in subsequent scenarios, python provides the partial function to generate a new function with some parameters.

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(10))# it's 21
#################

A is: 2 B is 3 c is 2
7
A is: 9 B is 10 c is 2
21

 

 

The following column uses partial to generate a decorator, which is used to modify the _ doc _ of the function as another function.

# Coding: UTF-8 ''' Created on 2014-10-1 @ author: zkchen ''' from functools import partialPARAMS = ("_ doc _",) def update_params (target, source, params = PARAMS): # Set the attribute mentioned by target in PARAMS to [setattr (target, p, getattr (source, p, None) of source )) for p in PARAMS] return targetdef update_params_wrap (source): return partial (update_params, source = source, params = PARAMS) def test_partial (): def funcA (): '''this is funcA's doc ''' pass @ update_params_wrap (funcA) def funcB (): ''' this is funcB's doc ''' pass print (funcB. _ doc __)
#################################

This is funcA's doc

 


How does one function use the variables in another function in python?

For example, if a function has a variable I, how can it be called or changed outside the function or in another function. Thank you for learning.

====================================

You cannot change the local variables in a function outside the function, because when you instantiate the function, the local variables in the function will be assigned a new value and redefined. How can you change the local variables?

If you want to implement all the variables and local variables, you must use global variables (self in the class ):

==================== Bianliang. py ================
# Coding: UTF-8
I = 0 # define a global variable I

Def ():
'''
Print the I value
'''
Print I

Def B ():
Global I

A () # Call method
Print I # print I
I = 1000 # re-assigning I
Print I # print I
A () # Call method a again

B ()
======================================

% Python bianliang. py
0
0
1000
1000

This py file uses a global variable I to implement the shared parameter I of method a and method B.

In python, functions are not enclosed in brackets.

In this way, you can see that x cannot be uploaded to B.
Def B (y ):
Return x + y

Def a (x ):
Return B

In fact, you call a --> Print the memory address of a, and a (x) is the method a called. The returned result is B, which is equivalent to printing the memory address of B directly,
So
A --> memory address of function
A (x) --> call method a and return function B object equivalent to --> B
A (x) (y) --> B (y) calls Method B to return the values of x and y. Here, x is the parameter value of method, y is the parameter value of Method B. This can understand the scope of the parameter.

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.