如何确定 Python 函数接受哪些参数?
How can I determine which arguments a Python function takes?
运行以下代码:
pdf = pdftotext.PDF(f,layout='raw')
产生了这个错误:
'layout' is an invalid keyword argument for this function
有没有办法列出这个函数和任何函数将采用哪些参数?
help
python 中的函数可用于查看 class 或函数的文档。在 python 中编码时,我喜欢保留 IPython
解释器 运行。 IPython
专门为此目的提供了 ?
和 ??
等运算符。
help(f) 显示 python 构造 f 的文档和参数,如 class 或函数。
例如在控制台上
help(print)
显示
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
帮助我的新功能 f ...
def f():
None
显示
Help on function f in module main:
f()
使用python的help内置函数。
help([object])
Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the
interactive help system starts on the interpreter console. If the
argument is a string, then the string is looked up as the name of a
module, function, class, method, keyword, or documentation topic, and
a help page is printed on the console. If the argument is any other
kind of object, a help page on the object is generated.
假设您来自 Python 2.7,并且需要有关 Python 的 print
功能的帮助 3. 转到交互式提示并键入 help(print)
:
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
(END)
如您所见,print
有 4 个关键字参数(sep
、end
、file
、flush
)。完成后按 q
退出。
运行以下代码:
pdf = pdftotext.PDF(f,layout='raw')
产生了这个错误:
'layout' is an invalid keyword argument for this function
有没有办法列出这个函数和任何函数将采用哪些参数?
help
python 中的函数可用于查看 class 或函数的文档。在 python 中编码时,我喜欢保留 IPython
解释器 运行。 IPython
专门为此目的提供了 ?
和 ??
等运算符。
help(f) 显示 python 构造 f 的文档和参数,如 class 或函数。
例如在控制台上
help(print)
显示
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
帮助我的新功能 f ...
def f():
None
显示
Help on function f in module main:
f()
使用python的help内置函数。
help([object])
Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.
假设您来自 Python 2.7,并且需要有关 Python 的 print
功能的帮助 3. 转到交互式提示并键入 help(print)
:
>>> help(print)
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. (END)
如您所见,print
有 4 个关键字参数(sep
、end
、file
、flush
)。完成后按 q
退出。