0

Is there an easy way to make class methods return the docstring of a function I wrote in a different module when help(my_classfunction) is called?

Steven
  • 404
  • 1
  • 3
  • 10
  • Can you give an example code what you are trying to do or what the expected behavior should be? – Christian Jun 06 '20 at 21:15
  • The docstring is available via `my_classfunction.__doc__`. `help` mostly formats the doc string (and for things like classes, gathers related doc strings as well). – chepner Jun 06 '20 at 21:28
  • @Christian I've written a module with functions that will be accessed using class methods. I've written it this way as the module should be portable. In this case, I have a class where the functions in the module have the functionality and are called by class methods (they are just an interface with the module). However, the end user should only interact with the class so I'd like for the user to be able to do help(my_classfunction) or my_classfunction.__doc__ and actually get the docstring of the function in the module, rather than the class method. Does that explain it a bit better? – Steven Jun 06 '20 at 22:38
  • @chepner yes, I'm aware of that. Naively, I tried putting the following in the class method, which didn't work: `my_classfunction.__doc__ = my_module.my_function.__doc__` after importing the module – Steven Jun 06 '20 at 22:39

1 Answers1

0

After digging a bit deeper, I found this related question that solved my problem. Turned out I had to use __func__ when setting the docstring of a classmethod. self.my_classmethod.__func__.__doc__ = my_module.my_function.__doc__ sets the docstring of the classmethod to the docstring of the function it calls.

Steven
  • 404
  • 1
  • 3
  • 10