Revised PEP 318 - Function/Method Decorator Syntax
Chris Liechti
cliechti at gmx.net
Wed Jun 11 18:18:40 EDT 2003
Erik Max Francis <max at alcyone.com> wrote in
news:3EE654A5.1F158A91 at alcyone.com:
> Chris Liechti wrote:
>
>> what's with the [] syntax discussed here a week ago?
>> it was something like:
>>
>> def foo(self) [synchronized(lock), classmethod]:
>
> To me it says, "Hi, I'm a list!" when listness does not seem warranted.
> Yes, we're talking about a sequence of properties, but the square
> brackets just look wrong there to me.
ok, so you dont like any sort of indexing neither ;-)
as in >>> 'hello'[3]
>> or merging with yours:
>>
>> def foo(self) as [synchronized(lock), classmethod]:
>
> Doubling up the syntaxes seems unnecessary.
>
>> i think the use of [] is quite logic. it's a _list_ of function
>> decorators.
>> and it helps readability, at least the variants should be listed as
>> alternatives in the PEP.
>
> It's a sequence. And, if you want to get technical, it's more likely to
> be handled as a tuple internally, anyway.
in that case it should be an iterator. "for" does nowdays work with
iterators and not sequences anymore, right? ;-)
in fact, we could take
def name(args) as <iterator>:
in cosideration. where "<iterator>" can be anything like in "for"
"for x in 1,2,3:", "for x in (1,):", "for x in [1,2,3]:"
"for x in range(3)", etc...
and that would in turn bring the mergend syntax back as "for x in 1:" is
not allowed neither. you have to use a list or a tuple (or an other
iterator)
what would be the advantage of allowing an iterator?
well, i'd say either you allow exactly one decorator, or a
list/sequence/iterator.
in both cases everything is possible, just that the "one only" solution
would require a decorator function that in turn takes a list of decoradors
if you need more than one at once.
as with exacly one decorator:
----
def decorator(iterable):
def decorate(function):
for d in iterable:
function = d(function)
return function
return decorate
def f() as decorator([d2, d3]):
pass
---
or what i would prefer:
---
def f() as [d2, d3]:
pass
---
when an iterator is allowed. i think almost all ppl agree that it should be
possible to specify more than one decorator at a time. i dont see an
argument for restricting it to a special new syntax if there is a keyword
between the "def" and the decorators. i'd use the same rules as for the
"for" loop.
things are different without the keyword. "def f(x) [d1, d2]:" here i would
require "[]" and nothing else. "()" is not good in case of only one
element, as Skip already pointed out.
btw. "as" is no keyword in the usual sense, you can bind a value to that
name... (does not shadow "import x as y")
>>> as = 1
so maybe it should be "in" to avoid new keywords...
"def f(x) in [d1, d2]:"
-> define function f with argument x in the list of decorators d1, d2
may not be 100% correct english, but it's a programming laguange anyway ;-)
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list