-
Notifications
You must be signed in to change notification settings - Fork 2
/
docstring.py
61 lines (45 loc) · 1.75 KB
/
docstring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python2.7
"""Copyright statement goes here
License goes here
Author goes here
This module presents how to use docstrigns according to google style
guide. It also demos that functions also have attributes.
You can use pydoc to see documentation for the file. You *must*
provide the modulename (filename w/o extension), otherwise you will
get "no Python documentation found for 'MODULENAME.py'" error
Using "pydoc -w" you can generate ugly-looking html page with docs
Pydoc will interpret your module. Use __name__ == "__main__" check to
avoid trouble
"""
def foo(greeting, rep=1):
"""Shows that even functions are objects in Python and how to
format docstring.
That's why you can access dostring as an attribute of a function.
Docstrings usually have oneline showrt destription followed by
empty line and longer description.Then you should see args, return
values, returned/generated value and raised exceptions.
You use foo.__doc__to access doc attribute of a funciton foo.
Args:
greeting: message printed by this funciton
rep: how many times the greeting is repeated (all in separate
lines). rep must be > 0
Returns:
number of bytes written to stdout
Raises:
AssertionError: rep must be positive
"""
assert rep > 0
for _ in xrange(rep):
print greeting
class MyClass:
"""Shows that classes also have docstring
Again, first line is short, then blank line followed by longer
description
"""
def __init__(self):
"""Methods get attributes in the same way as functions"""
pass
if __name__ == "__main__":
print "function docstring:", foo.__doc__
print "class docstring:", MyClass.__doc__
foo("hello world", rep=3)