0
def docstring(fname):
    print(fname.__doc__)

fname = input()
docstring(fname)

Given input:

print

The question is about to print the __doc__ of the print keyword, but here we can get output directly by command (print.__doc__) but while passing the variable (fname) to a function (docstring) and from there printing __doc__ showing different result as it is showing doc of variable but I need the doc of value in the variable.

Showing output:

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

Required output

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)\n\nPrints the values to a stream, or to sys.stdout by default.\nOptional keyword arguments:\nfile: a file-like object (stream); defaults to the current sys.stdout.\nsep: string inserted between values, default a space.\nend: string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jitendar
  • 1
  • 3
  • 1
    Does this answer your question? [How to convert a string to a function name in python?](https://stackoverflow.com/questions/7719466/how-to-convert-a-string-to-a-function-name-in-python) – Tomerikoo Aug 17 '20 at 15:50
  • Hum... makes sens to me. Could you explain more why you didn't expect this output ? – bastantoine Aug 17 '20 at 15:50
  • If you only expect to get built-in functions as input you can do `__builtins__[fname].__doc__` – Tomerikoo Aug 17 '20 at 15:54
  • `fname` is just a string (the result of the `input()` call). This is why you get the doc of string. If you want to convert the string to the actual function object (in order to get the doc), see the link or my suggestion above – Tomerikoo Aug 17 '20 at 16:04
  • Thank you so much @Tomerikoo – jitendar Aug 17 '20 at 16:10

1 Answers1

0

Use eval to get the right doc string printed.

def docstring(fname):
    return eval(fname + '.__doc__')

print(docstring('print'))
awadhesh pathak
  • 121
  • 1
  • 4