473,396 Members | 1,722 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,396 software developers and data experts.

decorators - more than just syntactic sugar

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

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Aug 11 '07 #1
11 2072
On Sat, 11 Aug 2007 20:30:54 +0200, Helmut Jarausch wrote:
are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?
They are just syntactic sugar.

@spam
def ham():
pass

is the same as

def ham():
pass

ham = spam(ham)
How can I find out the predefined decorators?
*The* predefined decorators? Decorators are just functions after all.
There are some meant to be used as decorators but there are also
"ordinary" functions that may make sense as decorators.

Ciao,
Marc 'BlackJack' Rintsch
Aug 11 '07 #2
On Aug 11, 8:30 pm, Helmut Jarausch <jarau...@skynet.bewrote:
Hi,

are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?
Well, I argued may times that syntactic sugar is important (all Turing
complete languages differs by syntactic sugar only) and having or not
having
a given syntactic sugar makes a difference between writing and not
writing
a given program. Having decorators in core Python makes us think
differently.
Have a look at David Mertz article

http://www.ibm.com/developerworks/li...l-cpdecor.html

Michele Simionato

Aug 13 '07 #3
On Aug 11, 8:30 pm, Helmut Jarausch <jarau...@skynet.bewrote:
How can I find out the predefined decorators?
I dare to say that's not easy. Since decorators are just(?)
syntactical sugar they don't obtain a particular semantics expressed
by distinctive declarative elements. Unlike generators which can be
identified looking for a yield expression a decorator can be
identified syntactically just when it is used. However this might be
sufficient, depending on your intention?

Aug 13 '07 #4
On 8/11/07, Helmut Jarausch <ja******@skynet.bewrote:
How can I find out the predefined decorators?
There are two in the standard library, @classmethod for declaring
class methods and @staticmethod for declaring static methods. They are
listed at the built ins page
http://docs.python.org/dev/lib/built-in-funcs.html, unpedagogically
not separated from ordinary functions.
--
mvh Björn
Aug 13 '07 #5
"BJörn Lindqvist" <bj*****@gmail.comwrote:
On 8/11/07, Helmut Jarausch <ja******@skynet.bewrote:
>How can I find out the predefined decorators?

There are two in the standard library, @classmethod for declaring
class methods and @staticmethod for declaring static methods. They are
listed at the built ins page
http://docs.python.org/dev/lib/built-in-funcs.html, unpedagogically
not separated from ordinary functions.

There are at least two other decorators in the standard library:

functools.wraps
contextlib.contextmanager

Aug 13 '07 #6
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
On Sat, 11 Aug 2007 20:30:54 +0200, Helmut Jarausch wrote:
>are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?

They are just syntactic sugar.

@spam
def ham():
pass

is the same as

def ham():
pass

ham = spam(ham)
Decorators are almost just syntactic sugar, but in fact there is a subtle
difference between the two bits of code you gave: in the second one you
assign the function to the name ham and then replace it with the result of
calling spam. With the decorator syntax you only have one assignment to ham
and when that decorator is called that assignment has not yet happened.

The difference is small, but you can write decorators which depend on it.
For example, the code I posted in <Xn*************************@127.0.0.1>
depends on this difference and will not work if you write the calls out
explicitly instead of using decorators.
Aug 13 '07 #7
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= <bj*****@gmail.comwrote:
>On 8/11/07, Helmut Jarausch <ja******@skynet.bewrote:
>How can I find out the predefined decorators?
There are two in the standard library, @classmethod for declaring
class methods and @staticmethod for declaring static methods. They are
listed at the built ins page
http://docs.python.org/dev/lib/built-in-funcs.html, unpedagogically
not separated from ordinary functions.
That page also demonstrates how property() can be used as a
decorator for conveniently creating read-only properties,
further emphasising the fact that a decorator really is an
ordinary function.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Aug 13 '07 #8
Michele Simionato <mi***************@gmail.comwrites:
On Aug 11, 8:30 pm, Helmut Jarausch <jarau...@skynet.bewrote:
>Hi,

are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?

Well, I argued may times that syntactic sugar is important (all Turing
complete languages differs by syntactic sugar only)
Although I agree that "mere" syntactic sugar matters, I think you're
overstating the point. I would argue that most people would understand
syntactic sugar as equivalent to a (very) localized program transformation.
Things like first class continuations clearly aren't syntactic sugar in that
sense.

'as
Aug 13 '07 #9
BJörn Lindqvist wrote:
unpedagogically not separated from ordinary functions.
Decorators _are_ ordinary functions. Remember the "syntactic sugar"
in this thread?

Regards,
Björn

--
BOFH excuse #338:

old inkjet cartridges emanate barium-based fumes

Aug 13 '07 #10
On Aug 13, 7:46 pm, Alexander Schmolck <a.schmo...@gmail.comwrote:
Michele Simionato <michele.simion...@gmail.comwrites:
Well, I argued may times that syntactic sugar is important (all Turing
complete languages differs by syntactic sugar only)

Although I agree that "mere" syntactic sugar matters, I think you're
overstating the point. I would argue that most people would understand
syntactic sugar as equivalent to a (very) localized program transformation.
Things like first class continuations clearly aren't syntactic sugar in that
sense.

'as
I don't think I am overstating my point. I am just pointing out
a sloppiness in the definition of "syntactic sugar". You are right
that most people understand it as °a somewhat trivial
program transformation". However most people tend to forget that
by a succession of somewhat trivial program transformations you
can get quite a lot. Look, a compiled language is just a whole big lot
of syntactic sugar over assembly language!
An even continuations can be implemented in terms of macros. the
quintessence of syntactic
sugar (as you know better than me). You are right that this
require a global program transformation, it is a kind of heavy
duty syntactic sugar, but still it is always syntactic sugar
at the end. Then, you may decide that you want to use a different
name from global program transformation, since syntactic sugar
sounds diminutive, but then it is an issue of names ;)

Michele Simionato

Aug 14 '07 #11
On 8/13/07, Bjoern Schliessmann
<us**************************@spamgourmet.comwrote :
BJörn Lindqvist wrote:
unpedagogically not separated from ordinary functions.

Decorators _are_ ordinary functions. Remember the "syntactic sugar"
in this thread?
Remember also "that syntactic sugar is important." Case in point, the
OP did not think of looking at the built-in functions page.

--
mvh Björn
Aug 14 '07 #12

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

Similar topics

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...
2
by: gohaku | last post by:
Hi everyone, The discussion on Python Decorators and "@" has piqued my interest on this "feature?" in programming languages such as Python and Java. can anybody what is the point in using...
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
9
by: Bengt Richter | last post by:
;-) We have @deco def foo(): pass as sugar (unless there's an uncaught exception in the decorator) for def foo(): pass foo = deco(foo) The binding of a class name is similar, and class...
23
by: Chance Ginger | last post by:
If I define a decorator like: def t(x) : def I(x) : return x return I and use it like: @t(X) def foo(a) :
10
by: David C. Ullrich | last post by:
What version added decorators (using the @decorator syntax)? (Is there a general way I could have found out the answer myself?) Is there a somthing such that "from __future__ import something"...
0
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bourdenas@gmail.com> escribi�: In a very strict sense, I'd say that all those references to "method decorators" are wrong - because...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.