Some things about decorator

Source: Internet
Author: User

#!/usr/bin/env python#--*--coding:utf-8--*--__author__ = ' Kevin ' def deco (func):    def In_deco (xx,yy):        print (' In Deco ')        func (xx,yy)    print (' Call deco ')    return in_deco# when the Python interpreter sees @deco, it calls the Deco function # incoming parameter bar, which is Deco ( BAR) and returns a function object In_deco#bar = In_deco#bar () In_deco () bar () #实质也是对闭包的使用 @decodef Bar (x, y):    print (' In bar ', x+y) print (Type (bar)) bar (ON)

#!/usr/bin/env python#--*--coding:utf-8--*--__author__ = ' Kevin ' def Dec (func):    print ' Call dec! '    Def in_dec (*arg):        print (' in Dec arg= ', arg)        if Len (arg) = = 0:            return 0 for Val in         a Rg:if not isinstance (val,int): Return 0 return func (*arg) print (' Return in_dec ') return in_dec# When the Python interpreter sees @dec, it will pass #my_sum as a parameter into the DEC # decorated function my_sum point to the new function in_dec# the essence of the adorner is the use of our closures @decdef my_sum (*arg): #my _sum = In_dec print (' in My_sum ') return sum (ARG) #my_sum = Dec (my_sum) Print (My_sum (1,2,3,4,5)) def My_ Average (*arg): return sum (ARG)/len (ARG)           

Some things about decorator

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.