Cute python:decorator simplified meta programming

Source: Internet
Author: User
Tags in python

Python makes metaprogramming possible, but every version of Python has a few nuances (and not completely compatible), which makes the path to metaprogramming more rugged. The use of a class of function objects has a long history, and there are also techniques for exploring and implementing magic properties. In version 2.2, Python added a helpful custom meta-class mechanism, but at the cost of racking up the user's brains. Recently, in version 2.4, Python added "decorator", the latest way to perform most metaprogramming--and the most user-friendly way to date.

Less labor, more.

Decorator has something in common with the metaprogramming abstractions introduced before Python: Even without these technologies, you can implement the functionality they provide. As Michele Simionato and I pointed out in an earlier article in the lovely Python column, even in Python 1.5, the creation of a Python class is possible without the need for a "meta-class" hook.

Decorator is fundamentally mediocre and very similar. The function implemented by decorator is to modify the functions and methods defined immediately after decorator. This is always possible, but this functionality is primarily driven by the Classmethod () and Staticmethod () built-in functions introduced in Python 2.2. In the old style, you can call Classmethod (), as follows:

Listing 1. Typical "old-fashioned" classmethod

class C:
  def foo(cls, y):
    print "classmethod", cls, y
  foo = classmethod(foo)

Although Classmethod () is a built-in function, it is not unique; You can also use your own methods to transform functions. For example:

Listing 2. Conversion of a typical "legacy" method

def enhanced(meth):
  def new(self, y):
    print "I am enhanced"
    return meth(self, y)
  return new
class C:
  def bar(self, x):
    print "some method says:", x
  bar = enhanced(bar)

What decorator does is prevent you from reusing the method name, and place the decorator where it is first mentioned in the method definition and its name. For example:

Listing 3. Typical "old-fashioned" classmethod

class C:
  @classmethod
  def foo(cls, y):
    print "classmethod", cls, y
  @enhanced
  def bar(self, x):
    print "some method says:", x

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.