A function in the Python class as an adorner

Source: Internet
Author: User

In the Python class, make a function of the adorner,

class A:     def Wrapper (func):   ## #装饰器        def wrapped (self,*arg,**Kwargs)             ...         return wrapped    @ wrapper              ## #装饰mix    def  Mix ():         ...

When the mix is called, Self.mix () will pass parameters such as self into the wrapper? The answer is no.

when wrapper as a decorator and @wrapper this way as a decoration , wrapper is like a normal function, just receives the decorated function as a parameter, and does not pass on the adorner any additional parameters such as SELF,CLS, The class is the equivalent of a namespace.

Wrapper itself with @staticmethod, and @classmethod decorate, will come to error.

' Staticmethod ' Object is not callable ' Classmethod ' Object is not callable

Because the adorner must be an object that can be called, and Wrapper is @staticmethod, and after the @classmethod is decorated, it is not a callable object.

A function in the Python class as an 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.