PEP-0318
John Roth
newsgroups at jhrothjr.com
Tue Aug 10 16:06:54 EDT 2004
"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.1459.1092154652.5135.python-list at python.org...
>
> >> Do people anticipate having lots of decorators for a given
function?
>
> Mark> The way they've been talking, it would appear so.
>
> Most of the lots-of-decorator examples were probably designed to make a
> competing decorator proposal look bad. ;-)
>
> In reality, I think the number of functions acquiring decorators will be
> fairly small unless you go whole hog for something like the
> accepts()/returns() example in PEP 318 or do lots of Objective C
interfacing
> as Bob Ippolito expects to do. In places where decorators will be used, I
> suspect their number will typically be smaller than the number of
parameters
> to the decorated functions. A quick scan of the standard library:
>
> find . -name '*.py' \
> | xargs cat \
> | egrep '^ *def .*\(.*\):$' \
> | sed -e 's/.*(//' -e 's/).*//' \
> | python -c 'import sys
> for l in sys.stdin:
> l = l.strip()
> if not l: print 0
> else: print len(l.split())
> ' | sort -n | uniq -c
>
> suggests that most functions have three or fewer parameters:
>
> 1096 0
> 8072 1
> 4024 2
> 1696 3
> 981 4
> 278 5
> 76 6
> 57 7
> 8 8
> 10 9
> 5 10
> 1 11
> 1 13
>
> Even if you account for functions whose parameter lists span multiple
lines
> I doubt that distribution will change very much.
>
> My guess is that use of decorators will be even more heavily weighted
toward
> 0, even after a number of Python releases.
One additional use case for decorators: prototype object systems.
I've been experimenting with Hans Nowak's system, and I am now
quite looking forward to having a single decorator that will bind the
function into an instance. For example:
@instancemethod(myinstance)
def go(self, wordList):
---stuff---
The result is that the function gets wrapped by new.instancemethod
and bound into the instance that's bound to myinstance.
For a program that uses this style of objects, just about every
module level function will be decorated.
John Roth
>
> Skip
More information about the Python-list
mailing list