12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

User-defined docstringsFor example, consider the following file, docstrings.py. Its docstrings appear at thebeginning of the file and at the start of a function and a class within it. Here, I’veused triple-quoted block strings for multiline comments in the file and the function,but any sort of string will work. We haven’t studied the def or class statements indetail yet, so ignore everything about them except the strings at their tops:"""Module documentationWords Go Here"""spam = 40def square(x):"""function documentationcan we have your liver then?"""return x **2class employee:"class documentation"passprint square(4)print square._ _doc_ _The whole point of this documentation protocol is that your comments are retainedfor inspection in _ _doc_ _ attributes, after the file is imported. Thus, to display thedocstrings associated with the module and its objects, we simply import the file andprint their _ _doc_ _ attributes, where <strong>Python</strong> has saved the text:>>> import docstrings16function documentationcan we have your liver then?>>> print docstrings._ _doc_ _Module documentationWords Go Here>>> print docstrings.square._ _doc_ _function documentationcan we have your liver then?>>> print docstrings.employee._ _doc_ _class documentation<strong>Python</strong> Documentation Sources | 281

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!