Coffeescript Implement Python Decorators

Source: Internet
Author: User
Tags javascript extension python decorator

In the Coffeescript–ngcomponent package of the previous angular, we talked about Coffeescript's small language, discarding the dross ("pit") part of JavaScript, And bring the pristine parts of JavaScript to dripping. Although I prefer the ES6 + Babel or typescript this kind of distinctive features of JavaScript syntax. But Coffeescript is also a good JavaScript extension language, especially in the Ruby community is still a good choice.

In this section, we will use Coffeescript to simulate the decorator implementation in Python. The python decorator is a class of AOP implementations that come from a language level. AOP is the abbreviation for aspect-oriented programming, meaning: aspect-oriented programming. A decoupling design of the common function and the business module is realized by using the compile-time (Compile) implant code, the runtime dynamic agent, and the framework to provide a pipeline execution strategy. AOP is a continuation of OOP, a hotspot in software development, and one of the core contents of many service-side frameworks (such as spring in the Java World), which is a derivative of functional programming. AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves development efficiency. AOP practical scenarios include: Permission control, log module, transaction processing, performance statistics, exception handling and other independent, general non-business modules. For more information about AOP, refer to http://en.wikipedia.org/wiki/Aspect-oriented_programming.

The decorator in Python is also a normal function, and we only need to use the @ symbol to label the method with another method signature to achieve the decoration of the method being labeled. This is also the use of higher-order functions in the functional language of Python. The decorated method will be introduced into the decorating function, which is decorated with a package to achieve the interception of the method.

The decorator in Python is as follows:

def deco(func):    def _deco(*args, **kwargs):        print("before %s called." % func.__name__)        ret = func(*args, **kwargs)        print("  after %s called. result: %s" % (func.__name__, ret))        return ret    return _deco@decodef myfunc(a, b):    print(" myfunc(%s,%s) called." % (a, b))    return a+b

Here the Decorator Deco, will wrap the MyFunc method, realize the call before and after the log information interception.

How do we achieve this in coffeescript? There is no real decorator feature in Coffeescript, but it has advanced functions that can be wrapped as follows:

log(myfunc)

In Coffeescript, we can also simplify the Remove method () symbol:

log myfunc

If we force the @ symbol again like Python and put the log function on the right side of the method declaration, it seems a little closer to the Python decorator:

f = @log (a, b) -> a + b

I do not know you as a reader, whether it is also a bit of decoration feeling it? Don't worry, we're looking at a complete demo example:

See the Pen coffeescript-decorator-Green (@greengerong) on Codepen.

Here we use the log function of the higher order function to wrap our custom functions. In fact, this is only the use of higher-order functions, if the syntax can also omit the method call (), it can also be achieved as above. Hopefully as readers you have come to understand the charm of higher-order functions in functional style, and its importance.

Coffeescript Implement Python Decorators

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.