473,425 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Document you code using docstrings.

bartonc
6,596 Expert 4TB
You will be rewarded sooner than you think when you use docstrings to document your classes and functions. Whether you ever intend to publish your work or not, docstrings will help you in several ways. First, let me show you the syntax:
Expand|Select|Wrap|Line Numbers
  1. def Add(a, b):
  2.     """Return the sum of two addable types.
  3.        This is a very simple example of docstring usage."""
  4.     return a + b
If you get into the habit of jotting down a short description of a function that you are writing (even before any of the body has taken shape), the first thing that you will be able to identify is whether you have factored the problem adaquately. If you can't describe the function in a line or two sentances, you may want to look at the problem again and see it you are not, in fact, trying to stuff too much into one function and should consider refactoring.
Secondly, docstrings are available in the Python shell via help() and dir(). So, if it's been a while since you have used one of your function you can import your library module and type
help(myLibModule.myFunc)
and your docstrings will be printed (this works for compiled modules, also).
Lastly, there are a few really cool automatic documentation tools that will read your module and make basic (but nice looking) documetation for you (but only if you use docstrings).
Jan 18 '07 #1
1 1643
bvdet
2,851 Expert Mod 2GB
Good advice Barton. You can also access the documentation with the __doc__ variable and attribute.
Expand|Select|Wrap|Line Numbers
  1. """
  2. This is a test of reading a documentation string.
  3. """
  4. def factorial(n):
  5.     """
  6.     This function returns the factorial of a number.
  7.     The factorial of 6 is 6*5*4*3*2.
  8.     This function uses recursion.
  9.     """
  10.     if n == 0 or n == 1:
  11.         return 1
  12.     else:
  13.         f = (n*factorial(n-1))
  14.     return f
  15.  
  16. print factorial(6)
  17. print factorial.__doc__
  18. print __doc__
  19.  
  20. >>> 720
  21.  
  22.     This function returns the factorial of a number.
  23.     The factorial of 6 is 6*5*4*3*2.
  24.     This function uses recursion.
  25.  
  26.  
  27. This is a test of reading a documentation string.
Jan 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Craig Ringer | last post by:
Hi folks I'm currently working on a fairly well internationalised app that embeds a Python intepreter. I'd like to make the docstrings translatable, but am running into the issue that the...
2
by: Steven Bethard | last post by:
I have two classes that implement the same interface, e.g. something like: class C(object): def foo(self): """Foo things""" ... def bar(self): """Bar things""" ... def baz(self):
0
by: Michael Muller | last post by:
I've observed something strange about property docstrings and I'm wondering if anyone here can clarify what's going on: if I create a class derived from property, the docstrings of the instances...
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
17
by: bearophileHUGS | last post by:
Hello, I know this topic was discussed a *lot* in the past, sorry if it bores you... >From the Daily Python-URL I've seen this interesting Floating Point Benchmark:...
0
by: Edward Loper | last post by:
Epydoc 3 supports extracting information about Python modules by parsing. As a result, it can extract "docstrings" for variables. There are several possible ways these docstrings could be...
12
by: jelle | last post by:
That's basically the idea... Often i find myself annotating what variables actually stand for, so to refer back to the code in half a year or so. # check if ID's or coords self.pointIDs = ptIDs...
9
by: Scott Huey | last post by:
I am working on a Python module and I would like to prepare some API documentaiton. I managed to find epydoc after some searching online. Is there a standard way to document the API for Python...
5
by: Matthew Wilson | last post by:
I wrote some code to create a user and update a user on a remote box by sending emails to that remote box. When I was done, I realized that my create_user function and my update_user function were...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.