Python Day4 function Adorner

Source: Internet
Author: User

Built-in functions:


Reference

Https://docs.python.org/2/library/functions.html




Decorative Device

Adorners are functions, except that the function can have special meanings, adorners are used to decorate functions or classes, and adorners can be used to add actions before and after a function is executed.

# define function, for call, function inside not execute

# function names > generation of reference functions


# @ + function name

function

# 1. Automatically executes the outer function and passes the function name F1 below as a parameter

# 2. The return value of the outer function is repeatedly assigned to F1



Example 1:


#!/usr/bin/env python# author:leon Wang Email: [Email protected]def outer (func): Def inner (*args,**kwargs): pri NT (' before ') R = func (*args,**kwargs) print ("after") return R return inner@outerdef F1 (ARG): PR Int (ARG) return "Comeon" @outerdef F2 (ARG1,ARG2): Print ("F2") ret=f1 ("Getdown") print ("Return value", ret) F2 (22,33)

Example 2:

#!/usr/bin/env python# author: leon wang email: [email protected] "def  F1 ():     print (123) Def f2 (a):     print (456) F2 (F1) "Def  login (func):     print ("Nb, passed user verification ...")      return funcdef home (name):     print ("Welcome [%s] to  home page " % name) @logindef  TV (name):     print (" welcome [%s ] to tv page " % name) Def movie (name):     print (" Welcome  [%s] to movie page " % name) #tv  = login (TV) TV (" Leon ")" Def login (func):     def inner (ARG):         print ("NB,  passed user verification.         func (ARG)      return&nBsp;innerdef home (name):     print ("Welcome [%s] to home page"  % name) @logindef  TV (name):     print ("Welcome [%s] to tv  page " % name) Def movie (name):     print (" Welcome [%s] to  movie page " % name) #tv  = login (TV) TV (" Leon ")


This article is from the "wind continue to blow" blog, please be sure to keep this source http://fengjixuchui.blog.51cto.com/854545/1786026

Python Day4 function Adorner

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.