Python帮助函数的断言

问题描述 投票:0回答:3

help 功能不能用于 assert为什么?

>>> help(assert)
  File "<stdin>", line 1
    help(assert)
         ^
SyntaxError: invalid syntax
python assert helper
3个回答
1
投票

要获得关键字的帮助,你需要传递关键字的字符串名称。

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

更多详情

调用内置帮助系统。(此函数用于交互式使用。)如果没有给出参数,交互式帮助系统在解释器控制台启动。如果参数是一个字符串如果参数是其他类型的对象,则会生成该对象的帮助页面。如果参数是任何其他类型的对象,则会生成该对象的帮助页。


1
投票

因为 assert 是一个语句,而不是一个 functionobject python的设计:为什么断言是语句而不是函数?

同样的情况也发生在

help(if)

0
投票

因为 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  --
© www.soinside.com 2019 - 2024. All rights reserved.