function attributes (ANN: Introducing PLY-1.0 (Python Lex-Yacc))

M.-A. Lemburg mal at lemburg.com
Wed Jun 20 09:34:05 EDT 2001


David Beazley wrote:
> 
> M.-A. Lemburg writes:
>  >
>  > Just a suggestion: PLY seems to use the same logic for attaching
>  > grammar snippets to functions as SPARK does. IMHO, this is bad
>  > design since doc-strings should really only be used for documentation
>  > and not include vital information for the program logic.
> 
> Actually, I thought the doc string hack was one of the neatest
> programming tricks I've ever seen (which is exactly why I copied it
> from SPARK).  Why would I write a different documentation string for a
> grammar rule anyways?  The grammar rule in the docstring not only
> tells the parser generator what to do, but it precisely documents what
> the function does at the same time.  I don't know what inspired John
> to take this approach in SPARK, but it's pure genius if you ask me :-).

IMHO, doc-strings are there to
document functions/methods in a human readable way, with additional
comments and maybe even usage examples. As such they are nice
to have around in the source, but are not necessarily needed
for program execution (e.g. python -OO removes them).

Maybe just me, but I believe that putting program logic into 
documentation is not a clean design.

>  > Note that in Python 2.1 we have function attributes which were
>  > added for exactly this reason, so the doc-string approach is
>  > not really needed anymore. I'd suggest to move to these for one
>  > of the next releases.
> 
> A fine idea, but the implementation is completely useless because
> there is no syntactically convenient way to attach the
> attributes. 

Ah... so that's what you're after: convenience !
That, of course, is true. 

So the conclusion should be: how can we make function attribute 
assignment more convienent and not why not to use them :-)

> IMHO, having to type something like this is an even more
> horrible design than using the docstrings (not to mention that it
> looks ugly and unnatural):
> 
>    def p_expr_plus(t):
>         t[0] = t[1] + t[3]
> 
>    p_expr_plus.grammar = 'expr : expr PLUS expr'
> 
> I don't have any plans to abandon the use of doc strings because I
> like the way that they work. If I were to use function attributes for
> anything, I would probably use them for some purpose other than
> grammar specification.  I'd have to think about that however.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/




More information about the Python-list mailing list