2007/06/06

python: comments

There are two styles of comments in python

    # single line comment

    " " " (this line MUST be valid indented in the block!)
    this is a multiline comment
    which spawns many lines
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    ...
    the next end line may be NOT indented!
    " " "

multiline comments are also used to embed what are called doc strings in a function, i.e.:

    class school:

    """ this class is useful for defining a type of school (1rst. level indented)
    with its strength,affiliation,address etc
    this class has following methods and properties
    affilitaion()
    address()
    """
    def affiliation(board):
    ....
    ...
    ...
    def address(add):
    ...
    ...
    ...

in above piece of code the triple qouted string not only is a comment
but when i declare an object say

    t = school

then,

    t.__doc__

or

    help(school)

will return those comments
-kiran

2 comments:

Sergey Shepelev said...

You have spaces between quotes in first example of multiline strings (btw they are not comments in any way).

Anonymous said...

Interesting to know.