473,573 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proposed improved decorator syntax


I'm revising my vote.

Thanks for the responses.

I vote against:

class foo:
@synchronized
@types("o,i,i")
@author('Chris King')
def introduceNewFea ture(self, someArgument, anotherArgument ):
pass # whatever

Pros:
- Allows arbitrary functions to be applied as decorators.

Cons:
- Places decorator metadata before the function (in contrast with docstrings).
- Hides function name.
- @ symbols are ugly.
- @ symbols are a particular hack for creating function metadata.
- More such hacks may be needed in the future.
- @ can be used to store metadata, but in a convoluted way:
@name('Value') works, if function 'name' is defined (imports must be used
for large projects); however the metadata name actually stored may be
different than 'name' (ie no symmetry, no ease of use).
- @ will be used for metadata, if no other solution is provided.

Recognize that we're *really* trying to create function metadata. A more
flexible solution is to make a metadata dictionary be part of function and
class definitions. Then 'decorator' simply becomes one name among many
different metadata names.

I vote for:

---------------------------------------------------------------------
Proposed Syntax
---------------------------------------------------------------------

class foo:
def introduceNewFea ture(self, someArgument, anotherArgument ):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever

Use introduceNewFea ture.__meta__ to access the metadata dict.
The .accepts meta-datum calls isinstance() for each argument.
Use 'object' for arguments that can be any type.

Pros:
- Readable.
- No ugly @ symbols.
- Metadata is placed after the function name.
- .decorate is semantically superior to @.
- Similar to existing Python syntax (equals sign and dots).
- Allows metadata to be created and read (symmetrically, easily).
- Allows arbitrary functions to be applied as decorators.
- Code is ordered correctly (function declaration before docstring and
metadata).

Cons:
- Name conflicts between builtin metadata names (eg 'decorate') and
user metadata names (eg 'decorate').

Solutions to Name Conflicts:
- Name conflicts are a small problem if the set of Python special
metadata names is kept small and well documented (eg '.decorate'
and '.accepts' can be documented as 'metadata keywords', to be
avoided).
- The paranoid can prefix metadata names with underscores.
- Alternatively, .__decorate__ and .__accepts__ can be used.
- Alternatively, {'decorate': [synchronized], 'accepts': (int, int)}
can be used as the first line of metadata, which is interpreted
as a 'Python special metadata dict'.

---------------------------------------------------------------------
Conclusion
---------------------------------------------------------------------

Either attack the full problem of metadata, or don't.

@ is a hack. @ looks ugly. @ is a particular solution for the general
problem domain of metadata. @ is not clear to the uninitiated. @ is
counter-intuitively present before the docstring. @name('Value') is
can be used to store metadata, but lacks ease of use and symmetry.

Finally, @ does not appear to follow the tradition of Python. It is a
very specialized extension statement, and at least deserves a descriptive
identifier in the code like 'decorate'.

Please don't stay silent on this issue. Speak out!

We should really have a public vote. I doubt the @ sign will be approved
if 99% of Python users oppose it.

- Connelly
Jul 18 '05 #1
5 1289
wrote in news:ma******** *************** *************** @python.org in
comp.lang.pytho n:
class foo:
def introduceNewFea ture(self, someArgument, anotherArgument ):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever


AIUI it is intended that prefix '.' will be used at some point
with a 'with'/'using' keyword:

class Foo:
def f( self ):
with self:
.member = value

But since we can have function's in function's and we already
have "special" member functions (__init__ etc):

class Foo:

def method( self ):

def __decorate__( func ):
synchronized( func )

__accepts__ = (int, int)
__author__ = "whoever"

pass

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 18 '05 #2
On 6 Aug 2004, Rob Williscroft wrote:
wrote in news:ma******** *************** *************** @python.org in
comp.lang.pytho n:
class foo:
def introduceNewFea ture(self, someArgument, anotherArgument ):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever


AIUI it is intended that prefix '.' will be used at some point
with a 'with'/'using' keyword:

class Foo:
def f( self ):
with self:
.member = value


Which makes it all the more intuitive (def is acting similar to a with
block).

Jul 18 '05 #3
Christopher T King wrote in news:Pine.LNX.4 .44.04080609173 30.30087-100000
@ccc6.wpi.edu in comp.lang.pytho n:
On 6 Aug 2004, Rob Williscroft wrote:
wrote in news:ma******** *************** *************** @python.org in
comp.lang.pytho n:
> class foo:
> def introduceNewFea ture(self, someArgument, anotherArgument ):
> .decorate = [synchronized]
> .accepts = (int, int)
> .author = 'Chris King'
> pass # whatever
>
>


AIUI it is intended that prefix '.' will be used at some point
with a 'with'/'using' keyword:

class Foo:
def f( self ):
with self:
.member = value


Which makes it all the more intuitive (def is acting similar to a with
block).


Intuitivly I would then expect .decorate to apply to self not to f.

But that's on a sample of 1 person's intuition :).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 18 '05 #4
barnesc wrote:
class foo:
def introduceNewFea ture(self, someArgument, anotherArgument ):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever
I like it better than the current syntax, but I'm troubled by stuff
after the ':' which is executed before the function.
It _looks_ like it is executed inside the function.

Also, the scoping is a bit strange:

def bar(self, baz):
.decorate = [baz]
pass # whatever

The decorator looks like it refers to the function parameter, but it
doesn't: the parameter has no value when the decorator is set. Though I
guess a simple fix in this case is simply to forbid decorators to use
names that occur as function parameters.

Rob Williscroft wrote:
But since we can have function's in function's and we already
have "special" member functions (__init__ etc):

class Foo:

def method( self ):

def __decorate__( func ):
synchronized( func )

__accepts__ = (int, int)
__author__ = "whoever"

pass


Same problem, only worse. The def statements of special member
functions are executed while the class body is executed, just like defs
of normal functions. OTOH, your special functions inside functions must
be executed once before the function is called.

--
Hallvard
Jul 18 '05 #5
On 08 Aug 2004 02:51:12 +0200, Hallvard B Furuseth
<h.**********@u sit.uio.no> wrote:
barnesc wrote:
class foo:
def introduceNewFea ture(self, someArgument, anotherArgument ):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever


I like it better than the current syntax, but I'm troubled by stuff
after the ':' which is executed before the function.
It _looks_ like it is executed inside the function.


This was Guido's reason for rejecting all forms that put the decorator
syntax inside the function block. They're _not_ part of the function's
code.
Jul 18 '05 #6

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

Similar topics

14
2293
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def __init__(self, name):
24
2042
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I see the following decisions that must be made: 1) Indicator Proposals differ on how some sort of indicator of "decoratorhood" is use. These...
11
1621
by: Ville Vainio | last post by:
It might just be that @decorator might not be all that bad. When you look at code that uses it it's not that ugly after all. A lot of the furor about this is probably because it happened so quicly. The situation might have been different if we had seen a pronouncement a week before, in the vein of "I have chosen this syntax - it will go in...
7
1576
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: http://mail.python.org/pipermail/python-dev/2004-August/047112.html http://mail.python.org/pipermail/python-dev/2004-August/047279.html
41
2833
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
17
1703
by: Jim Jewett | last post by:
Guido has said that he is open to considering *one* alternative decorator syntax. At the moment, (Phillip Eby's suggestion) J4 <URL: http://www.python.org/moin/PythonDecorators > (section 5.21 J4) looks very good to me -- and it is the only alternative without negatives. def func(arg1, arg2) @version("Added in 2.4") @returns(None)
22
2217
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
108
6341
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and...
13
1768
by: Lad | last post by:
I use Python 2.3. I have heard about decorators in Python 2.4. What is the decorator useful for? Thanks for reply L.
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8033
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8206
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5601
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2224
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.