Python Primer python 4 types: function decoration function, function decoration class, class decoration function, class decoration class

Source: Internet
Author: User

One: function decoration function
def Wrapfun (func):     def Inner (A, b):         Print ('function name:', func. __name__ )        = func (A, b)        return  R    return  Inner@wrapfundef  Myadd (A, B):    return A + bprint( Myadd (2, 3))

Second: Function Decoration class
defWrapclass (CLS):defInner (a):Print('class Name:'Cls.__name__)        returnCLS (a)returnInner@wrapclassclassFoo ():def __init__(Self, a): SELF.A=adefFun (self):Print('SELF.A =', SELF.A) m= Foo ('Xiemanr') M.fun ()

Three: Class decoration function
classshowfunname ():def __init__(Self, func): Self._func=funcdef __call__(Self, a):Print('function Name:', Self._func.__name__)        returnSelf._func (a) @ShowFunNamedefBar (a):returnaPrint(Bar ('Xiemanr'))

Four: Class decoration class
classShowclassname (object):def __init__(self, CLS): Self._cls=CLSdef __call__(Self, a):Print('class Name:', Self._cls.__name__)        returnSelf._cls (a) @ShowClassNameclassFoobar (object):def __init__(Self, a): Self.value=adefFun (self):Print(Self.value) a= Foobar ('Xiemanr') A.fun ()

Reference

Python Primer python 4 types: function decoration function, function decoration class, class decoration function, class decoration class

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.