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

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 introduceNewFeature(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 introduceNewFeature(self, someArgument, anotherArgument):
.decorate = [synchronized]
.accepts = (int, int)
.author = 'Chris King'
pass # whatever

Use introduceNewFeature.__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 1274
wrote in news:ma**************************************@pyth on.org in
comp.lang.python:
class foo:
def introduceNewFeature(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**************************************@pyth on.org in
comp.lang.python:
class foo:
def introduceNewFeature(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.0408060917330.30087-100000
@ccc6.wpi.edu in comp.lang.python:
On 6 Aug 2004, Rob Williscroft wrote:
wrote in news:ma**************************************@pyth on.org in
comp.lang.python:
> class foo:
> def introduceNewFeature(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 introduceNewFeature(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.**********@usit.uio.no> wrote:
barnesc wrote:
class foo:
def introduceNewFeature(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
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...
24
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...
11
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...
7
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: ...
41
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
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...
22
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...
108
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...
13
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.
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.