1

I have created a util python file that contains many functions in it. I want to use a long string to be used as a descriptive docstring for some of them, which can be shown with help(functionname). Is it possible to write the string just one time and use it for the desired functions (by referring that string from within the functions) as it can be shown with help?

For example (the main docstring is a long multiline string, here is just as an example):

longstring = """ It is the description """

def hello_():
    longstring
    return 'hello'

def by_():
    longstring
    return 'by'

# help(hello_) shows the longstring

I used a code similar to above, but that doesn't show that docstring.

maryam sh
  • 21
  • 3
  • Maybe check [how to programmatically set the docstring](https://stackoverflow.com/questions/4056983/how-do-i-programmatically-set-the-docstring). However, how useful is setting the same docstring to all your functions? – mozway Apr 03 '22 at 04:16
  • Docstrings exist at least in part to help people read your code, I'd avoid this unless you have a really pressing reason to do it – Patrick Haugh Apr 03 '22 at 04:18
  • @mozway Not to all functions. Some of them, for example, used the same specific library that leads to some limitations on some situations and ... and notes must be described for them. – maryam sh Apr 03 '22 at 05:39

1 Answers1

2

You can try function.__doc__ = "some string", but note this only works with functions.

Shiv
  • 370
  • 1
  • 14
  • It will work. But I am seeking for a way to use something like this within the functions as I showed in the example. Can I use this code line within the function or it must be used after function definition? – maryam sh Apr 03 '22 at 05:48
  • You can use it directly after function definition, but I don't think you can programmitacally set the docstring inside the function. – Shiv Apr 03 '22 at 16:31