Decorator in python and python decorator

Source: Internet
Author: User
Tags python decorator

Decorator in python and python decorator

What is a decoration device?

Assume that all functions A, B, and C have been compiled. In this case, you find that A, B, and C all require the same function. What should you do?

A: decorator

The decorator is actually a function, but the return value of this function is a function.

In my personal understanding, the decorator is mainly used to complete the above function. function A, function B, and Function C are wrapped in function D, and function D is executed before or after function, handle some things


#! /Usr/bin/env python # coding: utf-8def SeparatorLine (): print "###########################" # decorator with parameter functions and parameter def decratorArgFuncArg (f1, f2): def inner (func): def wrapper (arg): print "decorator with Parameter Function with parameter" f1 () result = func (arg) f2 () return result return wrapper return inner # decorator with Parameter Function Without parameter def DecratorArgFuncNoArg (f1, f2): def inner (func): def wrapper (): print "decorator with Parameter Function Without parameter" f1 () result = func () f2 () return result return wrapper return inner # function without parameter decorator def FuncNoArgDecrator (func): def wrapper (): print "decorator without parameters in the function" func () return wrapper # decorator def FuncArgDecrator (func): def wrapper (arg ): print "decorator with parameters in the function" func (arg) return wrapper # decorator def FuncReturnDecrator (func): def wrapper (): print "decorator with returned values in the function" result = func () return result return wrapper # the two functions use def login (): print 'to start logging on to 'def logout (): print 'logout logins '@ FuncArgDecratordef Lee (arg): print' I am % s' % arg @ FuncNoArgDecratordef Marlon (): print 'I am Marlon' @ DecratorArgFuncNoArg (login, logout) def Allen (): print 'I am Allen' @ DecratorArgFuncArg (login, logout) def Aswill (name): print 'I am % s' % name @ FuncReturnDecratordef Frank (): return 'I am frank' if _ name __= =' _ main _ ': SeparatorLine () Lee ('Lee') SeparatorLine () Marlon () SeparatorLine () allen () SeparatorLine () Aswill ('aswill ') SeparatorLine () result = Frank () print result



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.