Decorator in python and python decorator

Source: Internet
Author: User
Tags python decorator python static method

Decorator in python and python decorator

The decorator in python can decorate functions or classes. The function is to add some functions to functions or classes. Similar to the decoration mode in the design mode, it can separate the functional implementation part and the decoration part of the decorator to avoid redundant code in the class or function.

Decoration function:

def decrator(f1):def newf():print "strings will be connected:"print f1();return newf;@decratordef strconnect():x=raw_input("input the first string");y=raw_input("input the second string");return x+y;strconnect();

The code above adds a decorator to the strconnect function, and generates a new function newf in the decrator. The newf function body calls the f1 function and adds the decoration function.

Decorations:

def decrator(obj):class newclass():def __init__(self,s):self.tmp=obj(s);def show(self):print "apple is good for your health";print self.tmp.show();return newclass;@decratorclass apple():def __init__(self,s):self.str=s;def show(self):return self.str;t=apple('an apple a day keeps a doctor away');t.show();

Similar to decorating a function, the decorator can also be used as a decoration class. The decrator generates a new class newclass. The newclass constructor has an additional parameter s, used to generate the object of the decorated class, self. tmp = obj (s) implements this function. The show function in the decorator also calls the show function of the decorated class and adds the decoration code.

In addition to custom decorators, python also provides built-in decorators, such as static methods and class methods, which are implemented through the decorators. For more information about static methods and class methods, here:Python static method class Method.

The decorator decorated a function to return a new function. If it is decorated with a class, a new class is returned, which extends the functions of the original function or class.



How should I understand the python decorator?

The so-called decorator is to wrap the function and add some additional functions for the function. The decorator is a function. The parameter is the encapsulated function and the encapsulated function is returned: You can try again:
Def d (fp): def _ d (* arg, ** karg): print "do something before fp .. "r = fp (* arg, ** karg) print" do something after fp .. "return r return _ d @ ddef f (): print" call f "# The above @ d is used to indicate the decorator and the following: # f = d (f) f () # Call f

How can I use a decorator in python?

I don't know. Maybe I can't jump out of the main function in the decorator.

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.