The adorner mode of Python design mode

Source: Internet
Author: User

Adorner mode

Adorner mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.

This pattern creates an adornment class that wraps the original class and provides additional functionality, while preserving the integrity of the class method signature.

Import TimedefLog_calls (func):defWrapper (*args,**Kwargs): now=time.time ()Print("calling{0} with {1} and {2}". Format (func.__name__, Args,kwargs)) Return_value=func (*args,**Kwargs)Print("executrd{0} in {1}ms". Format (func.__name__, Time.time ()-Now )) returnReturn_valuereturnwrapperdefTest1 (A, b):Print("Test1 called") Time.sleep (1) Test1=log_calls (test1) test1 ()

The function passes in the adorner and returns a new function that assigns the function name of the original function to the new function, effectively replacing the original function with the decorated function.

The adorner mode of Python design mode

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.