Skip to content

Commit c476907

Browse files
committed
Updated docstring explainations and refs to use Google style.
1 parent ed08b5f commit c476907

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

exercises/concept/guidos-gorgeous-lasagna/.docs/hints.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
## 5. Update the recipe with notes
3939

4040
- Clearly [commenting][comments] and [documenting][docstrings] your code according to [PEP257][pep257] is always recommended.
41+
- Some examples of Google-style docstrings can be found in the Sphinx documentation for the [napoleon module][napoleon].
4142

4243
[assignment]: https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assignment-stmt
4344
[comments]: https://realpython.com/python-comments-guide/
@@ -46,6 +47,7 @@
4647
[docstrings]: https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings
4748
[magic-numbers]: https://en.wikipedia.org/wiki/Magic_number_(programming)
4849
[naming]: https://realpython.com/python-variables/
50+
[napoleon]: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#module-sphinx.ext.napoleon
4951
[numbers]: https://docs.python.org/3/tutorial/introduction.html#numbers
5052
[pep257]: https://www.python.org/dev/peps/pep-0257/
5153
[python as a calculator]: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator

exercises/concept/guidos-gorgeous-lasagna/.docs/instructions.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ Go back through the recipe, adding "notes" in the form of [function docstrings][
7878
```python
7979
def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
8080
"""Calculate the elapsed cooking time.
81-
82-
:param number_of_layers: int - the number of layers in the lasagna.
83-
:param elapsed_bake_time: int - time the lasagna has been baking in the oven.
84-
:return: int - total time elapsed (in minutes) preparing and baking.
81+
82+
Parameters:
83+
number_of_layers (int): The number of layers in the lasagna.
84+
elapsed_bake_time (int): Time the lasagna has been baking in the oven.
85+
86+
Returns:
87+
int: The total time elapsed (in minutes) preparing and baking.
8588
8689
This function takes two integers representing the number of lasagna
8790
layers and the time already spent baking the lasagna. It calculates
8891
the total elapsed minutes spent cooking (preparing + baking).
92+
8993
"""
9094
```
9195

exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,18 @@ Docstrings are declared using triple double quotes (""") indented at the same le
209209

210210
```python
211211

212-
# An example from PEP257 of a multi-line docstring.
212+
# An example from PEP257 of a multi-line docstring
213+
# reformatted to use Google style non-type hinted docstrings.
214+
# Some additional details can be found in the Sphinx documentation:
215+
# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#getting-started
216+
213217
def complex(real=0.0, imag=0.0):
214218
"""Form a complex number.
215219
216-
Keyword arguments:
217-
real -- the real part (default 0.0)
218-
imag -- the imaginary part (default 0.0)
220+
Keyword Arguments:
221+
real (float): The real part of the number (default 0.0)
222+
imag (float): The imaginary part of the number (default 0.0)
223+
219224
"""
220225

221226
if imag == 0.0 and real == 0.0:
@@ -224,36 +229,45 @@ def complex(real=0.0, imag=0.0):
224229
```
225230

226231

227-
Docstrings are read by automated documentation tools and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
228-
Docstring conventions are laid out in [PEP257][pep257].
232+
Docstrings are read by automated documentation tools such as [Sphinx][sphinx] and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
233+
General docstring conventions are laid out in [PEP257][pep257], but exact formats will vary by project and team.
234+
Exercism concept exercises try to follow the Google style for un-type hinted code.
229235

230236
Docstrings can also function as [lightweight unit tests][doctests], which will be covered in a later exercise.
231237

232238

233239
```python
234240
# An example on a user-defined function.
241+
# This uses Google style docstrings.
235242
>>> def raise_to_power(number, power):
236-
"""Raise a number to an arbitrary power.
237-
238-
:param number: int the base number.
239-
:param power: int the power to raise the base number to.
240-
:return: int - number raised to the specified power.
243+
"""Raise a number to an arbitrary power.
244+
245+
Parameters:
246+
number (int): The base number.
247+
power (int): The power to raise the base number to.
248+
249+
Returns:
250+
int: The number raised to the specified power.
251+
252+
Takes a number and raises it to the specified power, returning the result.
241253
242-
Takes a number and raises it to the specified power, returning the result.
243-
"""
254+
"""
244255

245-
return number ** power
256+
return number ** power
246257
...
247258

248259
# Calling the .__doc__ attribute of the function and printing the result.
249260
>>> print(raise_to_power.__doc__)
250261
Raise a number to an arbitrary power.
251262

252-
:param number: int the base number.
253-
:param power: int the power to raise the base number to.
254-
:return: int - number raised to the specified power.
263+
Parameters:
264+
number (int): The base number.
265+
power (int): The power to raise the base number to.
255266

256-
Takes a number and raises it to the specified power, returning the result.
267+
Returns:
268+
int: The number raised to the specified power.
269+
270+
Takes a number and raises it to the specified power, returning the result.
257271
```
258272

259273
[calls]: https://docs.python.org/3/reference/expressions.html#calls
@@ -271,4 +285,5 @@ Raise a number to an arbitrary power.
271285
[parameters]: https://docs.python.org/3/glossary.html#term-parameter
272286
[pep257]: https://www.python.org/dev/peps/pep-0257/
273287
[return]: https://docs.python.org/3/reference/simple_stmts.html#return
288+
[sphinx]: https://www.sphinx-doc.org/en/master/usage/index.html
274289
[type hints]: https://docs.python.org/3/library/typing.html

exercises/concept/guidos-gorgeous-lasagna/lasagna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def bake_time_remaining():
1616
"""Calculate the bake time remaining.
1717
18-
Parameters:\
18+
Parameters:
1919
elapsed_bake_time (int): The baking time already elapsed.
2020
2121
Returns:

0 commit comments

Comments
 (0)