简体   繁体   中英

How do I create a help function for a module created by me in PYTHON?

My code is:

import os

#This function renames the given file!
def rename(file_name, new_name):
     file_name = str(file_name)
     a = os.rename(file_name, new_name)
     return a


Now When I run my code in IDLE:

>>> help(rename)
Help on function rename in module __main__:

rename(file_name, new_name)
    #This function renames the given file!

>>> 

It returns like this!
If Code Like this:

import os

#This function renames the given file!
def rename(file_name, new_name):
    #In the file_name you have to give the file's name with extention!
    #In the new_name you have to give the new file's name with extention!
     file_name = str(file_name)
     a = os.rename(file_name, new_name)
     return a

And use help function in IDLE:

>>> help(rename)
Help on function rename in module __main__:

rename(file_name, new_name)
    #This function renames the given file!

>>> 

It returns the same!
Is there and way to print all the comments
when i use:

>>>help(rename)
Help on function rename in module __main__:

rename(file_name, new_name)
    #This function renames the given file!
    #In the file_name you have to give the file's name with extention!
    #In the new_name you have to give the new file's name with extention!

Like this?

What you need is a docstring . You use """ """ format to give your function a documentation that can be viewed with the help function.

You have to use docstrings. for example:

def rename(file_name, new_name):
    '''
    In the file_name you have to give the file's name with extention!
    In the new_name you have to give the new file's name with extention!
    '''
file_name = str(file_name)
    a = os.rename(file_name, new_name)
    return a

so when you call help(rename), you should get:

Help on function rename in module __main__:

rename(file_name, new_name)
    In the file_name you have to give the file's name with extention!
    In the new_name you have to give the new file's name with extention!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM