zoukankan      html  css  js  c++  java
  • Python 3 小知识 assert用法

    assert语句,如果没记错,这个东西在C或者C++里面也有的。属于短小的断言。下面的是来自python help document的说明:

    Assert statements are a convenient way to insert debugging assertions into a program:

    assert语句是一种插入调试断点到程序的一种便捷的方式。

    assert语句的使用格式

    assert expression

    这个语句是等价于下面的个句式:

    if __debug__:
        if not expression: raise AssertionError

    assert也可以用于多个表达式的断言

    assert expression1, expression2

    我自己写的一个关于质数判定的assert使用示例

    def isPrime(n):
        """This function return a number is a prime or not"""
        assert n >= 2
        from math import sqrt
        for i in range(2, int(sqrt(n))+1):
            if n % i == 0:
                return False
        return True

    assert使用起来还是很方便的,可以避免不必要的未知错误。

    伪python爱好者,正宗测试实践者。
  • 相关阅读:
    网址集合
    简单工具类-JsonUtil
    简单工具类-CookieUtils
    pom.xml
    jdbc.properties
    springmvc.xml
    applicationContext-redis.xml(spring整合redis集群)
    applicationContext-dao.xml
    web.xml
    环境变量配置及eclipse基本配置
  • 原文地址:https://www.cnblogs.com/herbert/p/2857233.html
Copyright © 2011-2022 走看看