【发布时间】:2020-09-06 18:07:59
【问题描述】:
help 函数不适用于assert?
为什么?
>>> help(assert)
File "<stdin>", line 1
help(assert)
^
SyntaxError: invalid syntax
【问题讨论】:
help 函数不适用于assert?
为什么?
>>> help(assert)
File "<stdin>", line 1
help(assert)
^
SyntaxError: invalid syntax
【问题讨论】:
因为assert 是一个声明。你可以help('assert')
输出:
>>> help('assert')
The "assert" statement
**********************
Assert statements are a convenient way to insert debugging assertions
into a program:
assert_stmt ::= "assert" expression ["," expression]
The simple form, "assert expression", is equivalent to
if __debug__:
if not expression: raise AssertionError
The extended form, "assert expression1, expression2", is equivalent to
if __debug__:
if not expression1: raise AssertionError(expression2)
These equivalences assume that "__debug__" and "AssertionError" refer
to the built-in variables with those names. In the current
implementation, the built-in variable "__debug__" is "True" under
normal circumstances, "False" when optimization is requested (command
line option "-O"). The current code generator emits no code for an
assert statement when optimization is requested at compile time. Note
that it is unnecessary to include the source code for the expression
that failed in the error message; it will be displayed as part of the
stack trace.
Assignments to "__debug__" are illegal. The value for the built-in
-- More --
【讨论】:
要获得关键字的帮助,您需要传递关键字的字符串名称
>>> help('assert')
The "assert" statement
**********************
Assert statements are a convenient way to insert debugging assertions
into a program:
assert_stmt ::= "assert" expression ["," expression]
The simple form, "assert expression", is equivalent to
if __debug__:
if not expression: raise AssertionError
The extended form, "assert expression1, expression2", is equivalent to
if __debug__:
if not expression1: raise AssertionError(expression2)
These equivalences assume that "__debug__" and "AssertionError" refer
to the built-in variables with those names. In the current
implementation, the built-in variable "__debug__" is "True" under
normal circumstances, "False" when optimization is requested (command
line option "-O"). The current code generator emits no code for an
assert statement when optimization is requested at compile time. Note
that it is unnecessary to include the source code for the expression
that failed in the error message; it will be displayed as part of the
stack trace.
Assignments to "__debug__" are illegal. The value for the built-in
variable is determined when the interpreter starts.
您只能在函数、类、模块或方法的对象上使用help。
>>> help(min)
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
如果您尝试对关键字使用帮助,您将收到语法错误,因为它们既不是对象也不是字符串
>>> help(assert)
SyntaxError: invalid syntax
>>> help(while)
SyntaxError: invalid syntax
>>> help(if)
SyntaxError: invalid syntax
调用内置帮助系统。 (此函数用于交互式使用。)如果没有给出参数,交互式帮助系统将在解释器控制台上启动。 如果参数是字符串,则将该字符串查找为模块、函数、类、方法、关键字或文档主题的名称,并在控制台上打印帮助页面。如果参数是任何其他类型的对象,则会生成有关该对象的帮助页面。
【讨论】:
因为 assert 是一个语句而不是一个函数/对象design of python: why is assert a statement and not a function?
同样的情况发生在
help(if)
【讨论】: