language lawyering - doc strings

Tim Peters tim.one at home.com
Fri Aug 31 21:02:23 EDT 2001


[gcash]
> Thanks, but I was looking for what allowed me to put a string as the
> first statement of a function or class.
>
> I know I *can*, but not because of anything the language spec says.

That isn't the problem:  section 8.2 says that a complete Python program has
the form

file_input:     (NEWLINE | statement)*

and if you trace back far enough you'll find that a plain string is a
legitimate statement.  Similarly, in

classdef:       "class" classname [inheritance] ":" suite

you'll find that a plain string can be the first statement in a suite.  And
so on.

What you *aren't* going to find is something saying that a plain string as
the first stmt of a suite is something special.  Indeed, this is legit
Python too:

def example():
    "I'm a docstring"
    "But I'm not"
    "And neither am I"
    6*2
    example
    "Me neither"

That function doesn't look terribly useful, but its meaning is defined (if
called, it does a little useless crap, then returns None).





More information about the Python-list mailing list