|
发表于 2022-2-17 20:24:39
|
显示全部楼层
本楼为最佳答案
本帖最后由 isdkz 于 2022-2-18 10:49 编辑
你如果想知道一个函数的返回值,你可以在 python shell 中使用 help() 函数,
help() 函数的用法有:
一:导入了这个模块的情况下,可以直接给 help() 传入函数对象
- import easygui
- help(easygui.textbox)
复制代码
二:你也可以不导入这个模块,需要给 help() 传入函数路径的字符串
可以得到以下结果:
Help on function textbox in easygui:
easygui.textbox = textbox(msg='', title=' ', text='', codebox=False, callback=None, run=True)
Displays a dialog box with a large, multi-line text box, and returns
the entered text as a string. The message text is displayed in a
proportional font and wraps.
Parameters
----------
msg : string
text displayed in the message area (instructions...)
title : str
the window title
text: str, list or tuple
text displayed in textAreas (editable)
codebox: bool
if True, don't wrap and width is set to 80 chars
callback: function
if set, this function will be called when OK is pressed
run: bool
if True, a box object will be created and returned, but not run
Returns
-------
None
If cancel is pressed
str
If OK is pressed returns the contents of textArea
>>>
Retruns 里面的即为返回值
从返回的结果中可以看到,如果按下取消键,返回None,
如果按下OK键,返回文本域中的内容,类型为字符串
我们还可以看到,help() 函数不仅返回了返回值的类型和描述,
还返回了这个函数的作用,参数的类型及默认值和对参数的描述,
help() 函数也可以对模块和类使用,help() 是我们学习 python 的一个好帮手
顺便告诉你一个小秘密,实际上,help() 是返回了对象的__doc__属性,不信你看:
- >>> import easygui
- >>> print(easygui.textbox.__doc__)
- Displays a dialog box with a large, multi-line text box, and returns
- the entered text as a string. The message text is displayed in a
- proportional font and wraps.
- Parameters
- ----------
- msg : string
- text displayed in the message area (instructions...)
- title : str
- the window title
- text: str, list or tuple
- text displayed in textAreas (editable)
- codebox: bool
- if True, don't wrap and width is set to 80 chars
- callback: function
- if set, this function will be called when OK is pressed
- run: bool
- if True, a box object will be created and returned, but not run
- Returns
- -------
- None
- If cancel is pressed
- str
- If OK is pressed returns the contents of textArea
- >>>
复制代码
而 __doc__ 属性其实是你在函数、类或模块开头加上的多行注释(注意这里的多行注释指的是注释类型,多行也可以单行),没有默认为None:
- >>> def test():
- ... ...
- ...
- >>> test.__doc__
- >>> def test():
- ... # test
- ... ...
- ...
- >>> test.__doc__
- >>> def test():
- ... '''
- ... test
- ... '''
- ... ...
- ...
- >>> test.__doc__
- '\n\ttest\n\t'
- >>> def test():
- ... '''test'''
- ... ...
- ...
- >>> test.__doc__
- 'test'
- >>>
复制代码
那既然这样,help() 还有什么存在的意义呢?
其实我也不知道,管他呢,存在即合理嘛
|
|