Python notes, python

Source: Internet
Author: User

Python notes, python

This article is a supplement to the Python notes -- decorator.

When using the decorator, some attributes of the decorated function will be lost, such as the following code:

 1 #!/usr/bin/env python 2  3 def deco(func): 4     def wrapper(): 5         print "Wrap start" 6         func() 7         print "Wrap end\n" 8     return wrapper 9 10 @deco11 def foo():12     """Docstring for foo"""13     print "In foo():"14 15 foo()16 print foo.__name__17 print foo.__doc__

The output is as follows:

$ python decorator_test.pyWrap startIn foo():Wrap endwrapperNone

We can see that the __name _ attribute is wrapper, while the _ doc _ attribute is None. This is inconvenient for common use. You cannot rewrite _ name _ and _ doc _ for every function using the decorator.

The update_wrapper and wraps provided by Python functools can effectively solve this problem. However, update_wrapper is called in the form of a method, while wraps encapsulates update_wrapper with a decorator. The sample code is as follows:

1 #! /Usr/bin/env python 2 from functools import update_wrapper 3 4 def deco (func): 5 def wrapper (): 6 print "Wrap start" 7 func () 8 print "Wrap end \ n" 9 return update_wrapper (wrapper, func) # Call the update_wrapper Method 10 11 @ deco12 def foo (): 13 "Docstring for foo" 14 print "In foo ():" 15 16 foo () 17 print foo. _ name _ 18 print foo. _ doc __
1 #! /Usr/bin/env python 2 from functools import wraps 3 4 def deco (func): 5 @ wraps (func) # Use the decorator to implement 6 def wrapper (): 7 print "Wrap start" 8 func () 9 print "Wrap end \ n" 10 return wrapper11 12 @ deco13 def foo (): 14 "Docstring for foo" 15 print "In foo ():" 16 17 foo () 18 print foo. _ name _ 19 print foo. _ doc __

The foo method decorated by deco can retain the previous _ name _ and _ doc _ attributes.

$ python decorator_test_with_update_wrapper.pyWrap startIn foo():Wrap endfooDocstring for foo$ python decorator_test_with_wraps.pyWrap startIn foo():Wrap endfooDocstring for foo

 


Can Python run perfectly in a notebook?

As long as any electronic device has the following features:
1. CPU (no matter what, including the mobile phone's ARM chip)
2. Run this intelligent operating system (no matter what it is, as long as it is an intelligent system)
Then this electronic device can run Python
Don't worry about the laptop. You can use it on your mobile phone ~
I want to learn Python ~ Convenient, simple, practical, and powerful ~~

Complete txt of Python learning notes

Python learning notes txt complete novel attachment has been uploaded to Baidu online disk, click to download for free:



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.