Differences between revisions 1 and 2
Revision 1 as of 2008-08-19 02:45:31
Size: 1349
Editor: pool-72-69-206-164
Comment:
Revision 2 as of 2008-08-19 10:07:08
Size: 2482
Editor: pool-72-69-206-164
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Line 5: Line 4:
'''''Must use Jython 2.5a1+ to run the examples contained within this article '''''Must use Jython 2.5a1+ to run the examples contained within this article '''''
Line 8: Line 7:

A new era in Jython development has arrived. We Jython developers will soon be able to begin harnessing many of the powerful features of the current Python language distribution. Using the latest alpha release of Jython (2.5a+), we can take a look at these new features today. Python is a language whose popularity has grown tremendously over the past several years, partially due to the great features that have been added. One of the newer features is decorators, which may appear daunting at first due to their odd syntax. As many Jython developers are also Java gurus, the syntax may look strikingly similar to the JavaEE Annotation. However, the Jython decorator is significantly different but equally as powerful.
A new era in Jython development has arrived. We Jython developers will soon be able to begin harnessing many of the powerful features of the current Python language distribution. Using the latest alpha release of Jython (2.5a+), we can take a look at these new features today. Python is a language whose popularity has grown tremendously over the past several years, partially due to the great features that have been added. One of the newer features are known as decorators, which may appear daunting at first due to their odd syntax. As many Jython developers are also Java gurus, the syntax may look strikingly similar to the JavaEE Annotation. However, the Jython decorator is significantly different but equally as powerful.
Line 13: Line 10:
Line 17: Line 13:

== Examples ==

The first example was taken from a the blog http://www.siafoo.net/article/68. It is probably the most straight forward example that I have found for decorators, and the one that helped me to understand them the best. Quick and to the point, this example shows the raw structure of what a basic decorator can accomplish.

{{{
def decorator_function(target):
    # Do something with the target function
    target.attribute = 1
    return target

def target(a,b):
    return a + b

# This is what the decorator actually does
target = decorator_function(target)
}}}

The following code has the same functionality, but it uses decorators. Basically, with the use of decorators you can omit the last line of the previous example.

{{{
def decorator_function(target):
    # Do something with the target function
    target.attribute = 1
    return target

#Here is the decorator with the syntax '@function_name'
@decorator_function
def target(a,b):
    return a + b

}}}

Both of the examples have the following result:

{{{
>>> target(1,3)
3
>>> target.attribute
1
}}}

*New* Jython Basics - Decorators on Jython

Submitted By: Josh Juneau

Must use Jython 2.5a1+ to run the examples contained within this article

Introduction

A new era in Jython development has arrived. We Jython developers will soon be able to begin harnessing many of the powerful features of the current Python language distribution. Using the latest alpha release of Jython (2.5a+), we can take a look at these new features today. Python is a language whose popularity has grown tremendously over the past several years, partially due to the great features that have been added. One of the newer features are known as decorators, which may appear daunting at first due to their odd syntax. As many Jython developers are also Java gurus, the syntax may look strikingly similar to the JavaEE Annotation. However, the Jython decorator is significantly different but equally as powerful.

What Is A Decorator?

You can do a Google search on Python decorators and find about a thousand articles, blogs, and tutorials on the subject. Rather than take up lots of your time with explanations, I will give give a basic definition and then provide a few examples of how can be used.

Decorator: A function that transforms another function. A function can be passed into a decorator, modified, and then returned.

Examples

The first example was taken from a the blog http://www.siafoo.net/article/68. It is probably the most straight forward example that I have found for decorators, and the one that helped me to understand them the best. Quick and to the point, this example shows the raw structure of what a basic decorator can accomplish.

def decorator_function(target):
    # Do something with the target function
    target.attribute = 1
    return target

def target(a,b):
    return a + b

# This is what the decorator actually does
target = decorator_function(target)

The following code has the same functionality, but it uses decorators. Basically, with the use of decorators you can omit the last line of the previous example.

def decorator_function(target):
    # Do something with the target function
    target.attribute = 1
    return target

#Here is the decorator with the syntax '@function_name'
@decorator_function
def target(a,b):
    return a + b

Both of the examples have the following result:

>>> target(1,3)
3
>>> target.attribute
1

JythonMonthly/Articles/August2008/1 (last edited 2009-04-11 22:31:44 by c-71-197-86-242)