top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to access Docstring of a function in python?

+2 votes
420 views
How to access Docstring of a function in python?
posted Jan 31, 2015 by Gnanendra Reddy

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Printing Docstring of function from inside the function

def myfunc():
    """ this is myfunc """
    doc = myfunc.__doc__
class MyClass:
    def bar(self):
       """ this is bar """
       doc = MyClass.bar.__doc__

Printing Docstring of function from inside the function: Alternate Method : similar method can be used from outside the function itself.

def myfunc():
    """ this is myfunc """
    doc = globals()["myfunc"].__doc__
class MyClass:
    def bar(self):
       """ this is bar """
       doc = globals()["MyClass"].bar.__doc__
answer Jan 31, 2015 by Salil Agrawal
Similar Questions
+2 votes

I want to take the file, "desktop/test.txt" and write it to "desktop/newfolder/test.txt". I tried the below script, and it gave me: "IOError: [Errno 2] No such file or directory: 'desktop/%s.txt'". Any suggestions would be great.

def firstdev(file):
 in_file = open("desktop/%s.txt") % file
 indata = in_file.read()
 out_file = open("desktop/newfolder/%s.txt", 'w') % file
 out_file.write(indata)
 out_file.close()
 in_file.close()

firstdev("test")
–1 vote

R has the function edit() which allows the editing of the definition of a function. Does python have something similar so that users can edit python functions on the fly?

Ref:
https://www.rdocumentation.org/packages/utils/versions/3.4.3/topics/edit

...