473,383 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Decorators

Christopher T. King suggested that "we're trying to kill too many birds
with one stone".

http://groups.google.com/groups?q=st...wpi.edu&rnum=1

He goes on to suggest three needs which decorators serve. Are these the
only purposes which are envisaged for decorators?

Most of the discussion has focused on syntax. I would like to see some
clarification of the purpose and meaning of a decoration.

The latest version of PEP 318 has a Motivation section which talks of
transforming a method or function. The possible purposes of the
transformation is not so clear.

The use of the term decorator is questionable. In everyday usage, to
decorate is to furnish with adornments. In this sense, we could say
that a docstring is a decorator, but is that true of a transformation.
It seems desirable that we choose words which are as close as possible
to their everyday usage.

Example 4 in the PEP 318 illustrates the enforcement of function/method
argument and return checking. Is it envisaged that the accepts and
returns functions become part of __builtins__? The code seems to assume
that the interpreter will divert the func arguments to accepts and the
return object to returns. Is this correct? If so, how many trigger
words are implemented?

Some syntactic options are provided in:
http://www.python.org/moin/PythonDecorators
I am inclined to support option D. The decorator follows the function
definition and is indented, indicating that the decorator qualifies the
decorated and is, in a sense subordinate to it. It doesn't use @.

Steve Bethard sets out the syntactic choices clearly:

http://groups.google.com/groups?q=be...gle.com&rnum=1

My choices would be:

Indicator: Keyword preferred, but not | (vertical bar), as it looks too
much like 1 or l.

Location: First line of the body. The decorators are subordinate to the
signature and indented like the docstring.

List notation: As ListType, with a line per entry.

Indented: Yes, no block.

Clearly, I don't fully understand PEP 318, but I wonder at times is
decorators aren't a solution in search of a problem.

Colin W.

Jul 18 '05 #1
12 1441
"Colin J. Williams" <cj*@sympatico.ca> wrote:
It seems desirable that we choose words which are as close as possible
to their everyday usage.


On the other hand, it's desirable to pick words which have established
meanings in computer science too. The concept of a "decorator" is
pretty well established. The term has been used in GUI frameworks for
10 or 15 years, and more recently has been enshrined in various pattern
collections.
Jul 18 '05 #2
Roy Smith <ro*@panix.com> pisze:
It seems desirable that we choose words which are as close as possible
to their everyday usage.


On the other hand, it's desirable to pick words which have established
meanings in computer science too. The concept of a "decorator" is
pretty well established. The term has been used in GUI frameworks for
10 or 15 years, and more recently has been enshrined in various pattern
collections.


In GUI frameworks it was used for other things IIRC. Also "decorator
pattern" in GOF means something different.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #3
"Colin J. Williams" <cj*@sympatico.ca> wrote in message news:<%A*********************@news20.bellglobal.co m>...
Christopher T. King suggested that "we're trying to kill too many birds
with one stone".

http://groups.google.com/groups?q=st...wpi.edu&rnum=1

He goes on to suggest three needs which decorators serve. Are these the
only purposes which are envisaged for decorators?


If I understand correctly, they'd be useful for anything where you'd
now use the syntax

function = decorator(function)

In addition to @staticmethod, you could have decorators for

(1) Memoization. Makes repeated function evaluation more efficient
without having to rewrite the function.

class memoize(object):
def __init__(self, func):
self.__func = func
self.__results = {}
def __call__(self, *args):
if args not in self.__results:
self.__results[args] = self.__func(*args)
return self.__results[args]

def fibonacci(n):
@memoize
if n in (0, 1):
return n
return fibonacci(n - 1) + fibonacci(n - 2)

(2) Debugging uses, like:

class printreturns(object):
"Behaves like f but prints its return values."
def __init__(self, f):
self.__f = f
def __call__(self, *args):
result = self.__f(*args)
if debug:
print 'f%r = %r' % (args, result)
return

def somefunc(x, y):
@printreturns
...
Jul 18 '05 #4


Roy Smith wrote:
"Colin J. Williams" <cj*@sympatico.ca> wrote:
It seems desirable that we choose words which are as close as possible
to their everyday usage.

On the other hand, it's desirable to pick words which have established
meanings in computer science too. The concept of a "decorator" is
pretty well established. The term has been used in GUI frameworks for
10 or 15 years, and more recently has been enshrined in various pattern
collections.

I guess that it depends partly on whether the majority of Python users
consider themselves to be computer scientists.

Colin W.

Jul 18 '05 #5
On Sat, 07 Aug 2004 18:40:47 -0400, Roy Smith <ro*@panix.com> wrote:
"Colin J. Williams" <cj*@sympatico.ca> wrote:
It seems desirable that we choose words which are as close as possible
to their everyday usage.


On the other hand, it's desirable to pick words which have established
meanings in computer science too. The concept of a "decorator" is
pretty well established. The term has been used in GUI frameworks for
10 or 15 years, and more recently has been enshrined in various pattern
collections.


My one line answer to the question of what it is I like about Python
is:

"""
It is literate without being effete.
"""

I can live with "decorator", because it is a word without a particular
meaning to me (in this general realm), and I can fill in that blank by
an understanding of what it means in concrete terms in Python. But it
disturbs me (mildly) to see it justified by something like the Gang of
Four reference or GUI framework stuff.

I have no compelling reason to read the GOF material. But feel myself
to be rarefied (by walking around kind of folks statndards) by
understanding, broadly, the reference. I can think of numbers of folks
I know who make a living in IT related work, and for whom I am
confident the reference would draw a blank.
..
I've written (simple) GUIs and never heard the word decorator used in
that context.

This is getting to be pretty effete stuff for a language that strives
to be for everybody. Or is that silliness finally off the table.

..
Standalone the issue of the justification of the word
"decorator"wouldn't motivate me to comment.
But then I just read something defending '@' because it evokes
"prepocessor". Which also then justifies, I guess, its position
relative to what it processes.

But it also - is it not - the thingy that proceeds "aol.com" when
writing to manny_man.

I am a great believer in (motivated ) folks capacity to learn new
things. And the existence of synonyms is not news to anyone. So no,
nobody motivated to learn new things will be stopped in their tracks
by @ in a new context (anymore than anyone motivated to learn new
things things would - given an explanation - be stopped in their
tracks by 1/2 = 0 ).

But there is something a bit effete about a confident statement that
'@' is consistent with prepended information by referencing its use
(somewhere apparently) to hold prepocessor directives..

The less effete argument, it seems to me, is the opposite, The method
is the addressee, the decorator the address. The @ therefore belongs
between them - at the top of the body of the method. Not before it.

Art
Jul 18 '05 #6
Arthur <aj******@optonline.com> wrote in message news:<u9********************************@4ax.com>. ..
On Sat, 07 Aug 2004 18:40:47 -0400, Roy Smith <ro*@panix.com> wrote: .... I am a great believer in (motivated ) folks capacity to learn new
things. And the existence of synonyms is not news to anyone. So no,
nobody motivated to learn new things will be stopped in their tracks
by @ in a new context
I agree.
(anymore than anyone motivated to learn new
things things would - given an explanation - be stopped in their
tracks by 1/2 = 0 ).


I never had a problem with 1/2 == 0; I was already used to it from C, C++, and Java.

But I did get stopped in my tracks the time a function that contained a line like

mean = sum(seq) / len(seq)

failed because the list happened not to contain any floats.
Jul 18 '05 #7
On 8 Aug 2004 04:16:09 -0700, da*****@yahoo.com (Dan Bishop) wrote:
Arthur <aj******@optonline.com> wrote in message news:<u9********************************@4ax.com>. ..
On Sat, 07 Aug 2004 18:40:47 -0400, Roy Smith <ro*@panix.com> wrote:

...
I am a great believer in (motivated ) folks capacity to learn new
things. And the existence of synonyms is not news to anyone. So no,
nobody motivated to learn new things will be stopped in their tracks
by @ in a new context


I agree.
(anymore than anyone motivated to learn new
things things would - given an explanation - be stopped in their
tracks by 1/2 = 0 ).


I never had a problem with 1/2 == 0; I was already used to it from C, C++, and Java.

But I did get stopped in my tracks the time a function that contained a line like

mean = sum(seq) / len(seq)

failed because the list happened not to contain any floats.


Not to get off on it.

But you should be aware that I am 100% in agreement with the good
arguments related to the this issue, and 100% against the bad ones.

;)
Art
Jul 18 '05 #8
Dan Bishop wrote:
"Colin J. Williams" <cj*@sympatico.ca> wrote in message news:<%A*********************@news20.bellglobal.co m>...
Christopher T. King suggested that "we're trying to kill too many birds
with one stone".

http://groups.google.com/groups?q=st...wpi.edu&rnum=1

He goes on to suggest three needs which decorators serve. Are these the
only purposes which are envisaged for decorators?

If I understand correctly, they'd be useful for anything where you'd
now use the syntax

function = decorator(function)

In addition to @staticmethod, you could have decorators for

(1) Memoization. Makes repeated function evaluation more efficient
without having to rewrite the function.

class memoize(object):
def __init__(self, func):
self.__func = func
self.__results = {}
def __call__(self, *args):
if args not in self.__results:
self.__results[args] = self.__func(*args)
return self.__results[args]

def fibonacci(n):
@memoize
if n in (0, 1):
return n
return fibonacci(n - 1) + fibonacci(n - 2)

(2) Debugging uses, like:

class printreturns(object):
"Behaves like f but prints its return values."
def __init__(self, f):
self.__f = f
def __call__(self, *args):
result = self.__f(*args)
if debug:
print 'f%r = %r' % (args, result)
return

def somefunc(x, y):
@printreturns
...


Dan,
Many thanks for this, the examples helped to clarify things.

I've attached a script which shows a significant time benefit for
memoize. Also, it shows about a 24% reduction in the elapsed time
as compared with version 2.3.

I had expected that the unwrapped version of fibonacci would be
better than the recursive method as it avoided the overhead of
function calls. This test shows the reverse.

Colin W.

# tFib.py
#example by Dan Bishop of decorator usage

class memoize(object):
def __init__(self, func):
self.__func = func
self.__results = {}
def __call__(self, *args):
if args not in self.__results:
self.__results[args] = self.__func(*args)
return self.__results[args]

@memoize
def fibonacci(n):
if n in (0, 1):
return n
return fibonacci(n - 1) + fibonacci(n - 2)

def Rfibonacci(n):
if n in (0, 1):
return n
return Rfibonacci(n - 1) + Rfibonacci(n - 2)

def Sfibonacci(n):
if n in (0, 1):
return n
return Sfibonacci(n - 1) + Sfibonacci(n - 2)
Sfibonacci= memoize(Sfibonacci)

def fib(n):
if n in (0, 1):
return n
i0= 1
i1= 2
lim= n/2 - 1
i= 0
while i < lim:
i+= 1
i0, i1= i0 + i1, i0 + i1 + i1
return (i0, i1)[n & 1]

if __name__ == '__main__':
from timeit import Timer
t = Timer("fib(10)", "from __main__ import fib")
print "Unwrapped, using fib: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("fibonacci(10)", "from __main__ import fibonacci")
print "Fibonacci, using @memoize: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("Rfibonacci(10)", "from __main__ import Rfibonacci")
print "Raw Fibonacci: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("Sfibonacci(10)", "from __main__ import Sfibonacci")
print "Fibonacci with old deco: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
'''
Results from the above using Windows XP 1.6MHz Pentium 4 with Python 2.4a

C:\Python24\python.exe tFib.py
Unwrapped, using fib: 4.81 usec/pass
Fibonacci, using @memoize: 2.81 usec/pass
Raw Fibonacci: 213.70 usec/pass
Fibonacci with old deco: 2.84 usec/pass

Results, using Python 2.3.3python -u "tFib.py" Unwrapped, using fib: 7.75 usec/pass
Raw Fibonacci: 255.84 usec/pass
Fibonacci with old deco: 3.23 usec/passExit code: 0


'''

Jul 18 '05 #9
Dan Bishop wrote:
"Colin J. Williams" <cj*@sympatico.ca> wrote in message news:<%A*********************@news20.bellglobal.co m>...

Christopher T. King suggested that "we're trying to kill too many birds
with one stone".

http://groups.google.com/groups?q=st...wpi.edu&rnum=1

He goes on to suggest three needs which decorators serve. Are these the
only purposes which are envisaged for decorators?


If I understand correctly, they'd be useful for anything where you'd
now use the syntax

function = decorator(function)


Many of the uses I'm seeing proposed for decorators would fit better
into the current syntax:

def func( ... )
pass
dosomething(func)

In other words, they're not specifically changing the behavior of the
function, but they are using the (name and/or address of the) function
in some other context. The modification of the function (or method)
itself is an unnecessary side effect.

Whether this distinction matters is obviously a purely aesthetic matter,
since obviously the decorator can rebind the name to the original
function object instead of a wrapping-function object. But still, it
does suggest that this concept is indeed being put to multiple
orthogonal purposes.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #10
Jeff Shannon wrote:
Dan Bishop wrote:
"Colin J. Williams" <cj*@sympatico.ca> wrote in message
news:<%A*********************@news20.bellglobal.co m>...

Christopher T. King suggested that "we're trying to kill too many
birds with one stone".

http://groups.google.com/groups?q=st...wpi.edu&rnum=1
He goes on to suggest three needs which decorators serve. Are these
the only purposes which are envisaged for decorators?
If I understand correctly, they'd be useful for anything where you'd
now use the syntax

function = decorator(function)


Many of the uses I'm seeing proposed for decorators would fit better
into the current syntax:

def func( ... )
pass
dosomething(func)

In other words, they're not specifically changing the behavior of the
function, but they are using the (name and/or address of the) function
in some other context. The modification of the function (or method)
itself is an unnecessary side effect.

It seems to me that PEP 318 is intended to be transformational, as in
Dan Bishop's examples.

The purpose is to flag the transformation at the head of the function
declaration, ie. to give notice to the reader that the function is not
what it seems to be.

One of the issues is whether the flagging should occur before or after
the function signature. I can see some merit in the flagging and feel
that it better after the signature, where the function name is, in a
sense known.

Another issue is whether a single character or a keyword is used to
flag the 'decorator'. One post suggested that the pie would be better
used as a replacement for self.

There is also the question of whether 'decorator', a term which is used
in everyday langage as indicating adornment, is the best choice for a
transformation.
Whether this distinction matters is obviously a purely aesthetic matter,
since obviously the decorator can rebind the name to the original
function object instead of a wrapping-function object. But still, it
does suggest that this concept is indeed being put to multiple
orthogonal purposes.
Jeff Shannon
Technician/Programmer
Credit International

Colin W.

Jul 18 '05 #11
On Mon, 09 Aug 2004 20:29:59 -0400, Colin J. Williams <cj*@sympatico.ca> wrote:

There is also the question of whether 'decorator', a term which is used
in everyday langage as indicating adornment, is the best choice for a
transformation.


def deco(f):
print f.__name__, "is now pretty!"
return f

@deco
def ugly_function():
pass
Jul 18 '05 #12
In article <ma**************************************@python.o rg>,
Andrew Durdin <ad*****@gmail.com> wrote:
On Mon, 09 Aug 2004 20:29:59 -0400, Colin J. Williams <cj*@sympatico.ca> wrote:

There is also the question of whether 'decorator', a term which is used
in everyday langage as indicating adornment, is the best choice for a
transformation.
def deco(f):
print f.__name__, "is now pretty!"
return f


def deco (f):
print f.__name__, "is now pretty!"
def t (*s,**k):
print """\
I'm so pretty, oh so pretty!
I'm so pretty and witty and bright!
"""
return t

@deco
def ugly_function():
pass

Throw in an f(*s,**k) there if you really feel you must.
Regards. Mel.
Portions copyright by Stephen Sondheim or somebody.
Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
8
by: Michele Simionato | last post by:
Decorators can generate endless debate about syntax, but can also be put to better use ;) Actually I was waiting for decorators to play a few tricks that were syntactically too ugly to be even...
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator. One of the issues is that the Decorator pattern is...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
13
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such a thing called decorators ? tia KM
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.