Python's Functools module

Source: Internet
Author: User

This module provides 3 interesting functions, which are described below for their usage.

The first is the partial function, which can rebind the optional parameters of the function to generate a callable partial object

  1. >>> Int (' ten') # is actually equivalent to int (' Ten ', base=10) and int (' Ten ', ten)
  2. Ten
  3. >>> Int (' ten ', 2) # is actually an abbreviation for int (' Ten ', base=2)
  4. 2
  5. >>> from functools import partial
  6. >>> int2 = partial (int, 2) # I didn't write base here, and it went wrong.
  7. >>> Int2 (' ten ')
  8. Traceback (most recent):
  9. File "<stdin>", line 1, in <module>
  10. Typeerror:an integer is required
  11. >>> int2 = partial (int, base=2) # bind the base parameter to the Int2 function
  12. >>> int2 (' ten ') # Now the default parameter base is set to 2
  13. 2
  14. >>> Int2 (' Ten ', 3) # without base, the result is wrong
  15. Traceback (most recent):
  16. File "<stdin>", line 1, in <module>
  17. typeerror:keyword parameter ' base ' was given by position and by name
  18. >>> Int2 (' Ten ', base=3)
  19. 3
  20. >>> Type (int2)
  21. <type ' functools.partial '>

As you can see, the only thing to note is that an optional parameter must write out the parameter name.

Next is the Update_wrapper function, which can copy the encapsulated function's __name__, __module__, __doc__, and __dict__ to the encapsulation function:

  1. #-*-coding:gbk-*-
  2.   
  3. def thisisliving (fun):
  4. def Living (*args, **kw):
  5. return fun (*args, **kw) + ' Alive is eating. '
  6. return living
  7.  
  8. @thisIsliving
  9. def whatisliving ():
  10. "What Is Alive"
  11. return ' Yes, what's the life? '
  12.   
  13. Print whatisliving ()
  14. print whatisliving.__doc__
  15.   
  16. print
  17.   
  18. From functools import update_wrapper
  19. def thisisliving (fun):
  20. def Living (*args, **kw):
  21. return fun (*args, **kw) + ' Alive is eating. '
  22. return update_wrapper (living, fun)
  23.  
  24. @thisIsliving
  25. def whatisliving ():
  26. "What Is Alive"
  27. return ' Yes, what's the life? '
  28.   
  29. Print whatisliving ()
  30. print whatisliving.__doc__

Results:
Yes
, what's the life? To live is to eat.
None

Yes, what's the life? To live is to eat.
What is Alive
However, it is not much use, after all, just a few 4 lines to write the assignment statement.

Finally, the wraps function, which encapsulates the Update_wrapper, is also included:

[Python]View Plain Copy
  1. #-*-coding:gbk-*-
  2.   
  3. From functools import wraps
  4.   
  5. def thisisliving (fun):
  6. @wraps(fun)
  7. def Living (*args, **kw):
  8. return fun (*args, **kw) + ' Alive is eating. '
  9. return living
  10.  
  11. @thisIsliving
  12. def whatisliving ():
  13. "What Is Alive"
  14. return ' Yes, what's the life? '
  15.   
  16. Print whatisliving ()
  17. print whatisliving.__doc__

Python's Functools module

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.