簡體   English   中英

Python 幫助 function 用於斷言

[英]Python help function for assert

help function 不可用於assert 為什么?

>>> help(assert)
  File "<stdin>", line 1
    help(assert)
         ^
SyntaxError: invalid syntax

因為斷言是語句而不是python 的函數/對象設計:為什么斷言語句而不是 function?

同樣的情況發生在

help(if)

要獲得關鍵字的幫助,您需要傳遞關鍵字的字符串名稱

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

更多細節

調用內置幫助系統。 (此 function 旨在用於交互式使用。)如果沒有給出參數,交互式幫助系統將在解釋器控制台上啟動。 如果參數是 string ,則將該字符串查找為模塊的名稱、function、class、方法、關鍵字或文檔主題,並在控制台上打印幫助頁面。 如果參數是任何其他類型的 object,則會生成 object 上的幫助頁面。

因為assert是一個聲明。 你可以做help('assert')

output:

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

暫無
暫無

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

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