docstrings.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Docstirng examles from https://realpython.com/documenting-python-code/
  2. class Animal:
  3. """
  4. A class used to represent an Animal
  5. ...
  6. Attributes
  7. ----------
  8. says_str : str
  9. a formatted string to print out what the animal says
  10. name : str
  11. the name of the animal
  12. sound : str
  13. the sound that the animal makes
  14. num_legs : int
  15. the number of legs the animal has (default 4)
  16. Methods
  17. -------
  18. says(sound=None)
  19. Prints the animals name and what sound it makes
  20. """
  21. says_str = "A {name} says {sound}"
  22. def __init__(self, name, sound, num_legs=4):
  23. """
  24. Parameters
  25. ----------
  26. name : str
  27. The name of the animal
  28. sound : str
  29. The sound the animal makes
  30. num_legs : int, optional
  31. The number of legs the animal (default is 4)
  32. """
  33. self.name = name
  34. self.sound = sound
  35. self.num_legs = num_legs
  36. def says(self, sound=None):
  37. """Prints what the animals name is and what sound it makes.
  38. If the argument `sound` isn't passed in, the default Animal
  39. sound is used.
  40. Parameters
  41. ----------
  42. sound : str, optional
  43. The sound the animal makes (default is None)
  44. Raises
  45. ------
  46. NotImplementedError
  47. If no sound is set for the animal or passed in as a
  48. parameter.
  49. """
  50. if self.sound is None and sound is None:
  51. raise NotImplementedError("Silent Animals are not supported!")
  52. out_sound = self.sound if sound is None else sound
  53. print(self.says_str.format(name=self.name, sound=out_sound))