簡體   English   中英

如何為我在 PYTHON 中創建的模塊創建幫助 function?

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

我的代碼是:

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


現在當我在 IDLE 中運行我的代碼時:

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

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

>>> 

它像這樣返回!
如果代碼是這樣的:

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

並在 IDLE 中使用幫助 function:

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

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

>>> 

它返回相同的!
有沒有辦法打印所有的評論
當我使用:

>>>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!

像這樣?

你需要的是一個docstring 您使用 """ """ 格式為您的 function 提供可以在 function 幫助下查看的文檔。

您必須使用文檔字符串。 例如:

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

所以當你調用 help(rename) 時,你應該得到:

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!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM