Python中的assert


缘由:

在逛blog的时候看到下面的代码中用到了不少 assert ,虽然知道字面意思,但不清楚具体的作用以及优点,因此,有必要学习一下。

import os
from hashlib import sha256
from hmac import HMAC

def encrypt_password(password, salt=None):
    """Hash password on the fly."""
    if salt is None:
        salt = os.urandom(8) # 64 bits.

    assert 8 == len(salt)
    assert isinstance(salt, str)

    if isinstance(password, unicode):
        password = password.encode('UTF-8')

    assert isinstance(password, str)

    result = password
    for i in xrange(10):
        result = HMAC(result, salt, sha256).digest()

    return salt + result

hashed = encrypt_password('secret password')    # binary data

def validate_password(hashed, input_password):
    return hashed == encrypt_password(input_password, salt=hashed[:8])

assert validate_password(hashed, 'secret password')
搜索关键字:

python assert

参考链接:
参考解答:

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)

##

The assert statement exists in mostly every other programming language out there. When you do…

assert a_condition

… you’re telling the program to test that condition, and trigger an error if the condition is false.

In Python, it’s roughly equivalent to this:

if not condition:
    raise AssertionError()

##

  1. assert语句用来声明某个条件是真的
  2. 如果你非常确信某个你使用的列表中至少有一个元素,而你想要检验这一点,并且在它非真的时候引发一个错误,那么assert语句是应用在这种情形下的理想语句。
  3. 当assert语句失败的时候,会引发AssertionError。

##

下面是我建议的不要用断言的场景:
  • 不要用它测试用户提供的数据。
  • 不要用断言来检查你觉得在你的程序的常规使用时会出错的地方断言是用来检查非常罕见的问题。你的用户不应该看到任何断言错误,如果他们看到了,这是一个bug,修复它。
  • 有的情况下,不用断言是因为它比精确的检查要短,它不应该是懒码农的偷懒方式。
  • 不要用它来检查对公共库的输入参数,因为它不能控制调用者,所以不能保证调用者会不会打破双方的约定。
  • 不要为你觉得可以恢复的错误用断言。换句话说,不用改在产品代码里捕捉到断言错误。
  • 不要用太多断言以至于让代码很晦涩。

=EOF=

,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注