Decorators

J

Johny

Hi,
Can anyone explain to me what are decorators for? What are advantages
of using them?
Thanks
L.
 
S

Steven D'Aprano

Johny said:
Hi,
Can anyone explain to me what are decorators for? What are advantages
of using them?

Decorators are for many things. One thing is for factoring out common
functionality. Suppose, for example, you have a number of functions that
all test that the argument is larger than zero. Here's two:

def spam(n):
if n <= 0:
raise ValueError("argument must be positive")
return "spam "*n

def spammier(n):
if n <= 0:
raise ValueError("argument must be positive")
return "spam spam wonderful SPAM "*n

Imagine you have lots of them. The common code is wasteful, tedious to type,
and it breaks the DRY principle ("Don't Repeat Yourself"). If you decide
that the functions need a different test, you have to change every single
function, which is prone to errors -- it's easy to miss one, or make a
mistake. So let's factor the common code out, using a decorator:

def test_argument(some_function):
# Decorator that checks that the argument to some_function is > 0.
def func(arg):
# The code common to all functions.
if n <= 0:
raise ValueError("argument must be positive")
# Now call the decorated function.
return some_function(arg)
return func

Notice that the decorator "test_argument" returns the function object "func"
itself, not the result of calling func().

Now let's see how to use it, first the old-fashioned way:

def spam(n):
return "spam "*n

spam = test_argument(spam)


This is so common that Python uses special syntax to simplify it.

@test_argument
def spammier(n):
return "spam spam wonderful SPAM "*n



Now if you need to change the test, say to ensure that n is at least five
instead of one, you can change it in a single place, inside the decorator.
 
T

Terry Reedy

Johny said:
Hi,
Can anyone explain to me what are decorators for? What are advantages
of using them?

There are two questions: why wrap or modify a function after it is
modified? (others have answered this), and why the special syntax?

As to the second:

@deco
def f(): pass

is syntactic sugar for

def f(): pass
f = deco(f)

with two advantages. The 'sugar' informs the reader immediately that
the function will be wrapped or modified, which could be missed if the
body is long, and eliminates two repetitions of the function name, which
could be a long_info_rich_coded_name such as required in some projects.

A subtle effect of having the special syntax is that it has directed
more attention to the idea of post-processing functions and now, in 3.0,
classes.

Terry Jan Reedy
 
K

Krishnakant

Hello all,
I am looking out for a python library which does the followingg.
1, create and manipulate openoffice spreadsheets.
2, allow for cell formatting including merging cells.
3, allow for colouring cells and formatting data as bold italics etc.
4, alignment of data should be possible.
I looked at ooolib but did not find a method for merging cells.

any ideas?

happy hacking.
Krishnakant.
 
T

Terry Reedy

Krishnakant said:
Hello all,
I am looking out for a python library which does the followingg.
1, create and manipulate openoffice spreadsheets.

OpenOffice, by default, creates ODF -- Open Document Format -- documents
as zipped xml subdocuments. Ooo is just one of many programs that work
with them. One can access the actual xml with any decent zip program,
such as 7zip.
2, allow for cell formatting including merging cells.
3, allow for colouring cells and formatting data as bold italics etc.
4, alignment of data should be possible.
I looked at ooolib but did not find a method for merging cells.
any ideas?

odf2py? Have not used it yet. To get an idea of how to do what you
want to do, create a simple spreadsheet with merged and formatted cells
with OOoCalc and then look at the resulting xml rendition.


tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top