在Python项目中禁用assert的策略及原理是什么
Admin 2022-08-11 群英技术资讯 278 次浏览
在潜意识中, assert 是应用在unittest或pytest环境中, 不能应用到业务代码中, 因为断言会导致运行中断,对业务有损,并且消耗内存, 影响性能.
但不可否认, 使用断言非常方便调试代码
通过研读assert的文档, 发现断言是可以被关闭的,特此记录下
详细的介绍参考这个文档: 参考这篇文档
有两种方案
在python 内部机制中, 有一常量__debug__,值为BOOL类型的, 与断言是强关联的, 因为__debug__是常量, 所以当python 解释器运行时, 不能修改它的值.
Python 3.8.13 (default, Mar 28 2022, 06:16:26) [Clang 12.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> __debug__ True >>> __debug__ = False File "<stdin>", line 1 SyntaxError: cannot assign to __debug__ >>>
既然运行时不能修改, 那没有运行时可以修改吗?当然可以的, 这取决于python 运行模式,python运行模式有两种,和__debug__的关系如下表所示:
Mode | Value of debug |
---|---|
Normal (or debug)(调试模式) | True |
Optimized(优化模式) | False |
那__debug__的值和断言有什么关系呢?
看源码:
if __debug__: if not expression: raise AssertionError(assertion_message)
所以__debug__==True时会抛出断言异常, __debug__==False时就啥也不干,跳过了
这下清晰了
验证方案:
1、先写这样一个测试脚本,命名为demo.py
# demo.py print(f"__debug__=={__debug__}"
2、在命令行使用 禁用断言的启动方式
python -O demo.py
输出结果
__debug__==False
辅助理解: 在命令行中运行python -h,其结果也体现了这样的使用方式
mac$ python -h usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors) -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x -I : isolate Python from the user's environment (implies -E and -s) -m mod : run library module as a script (terminates option list) -O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x -OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension -q : don't print version and copyright messages on interactive startup -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE -S : don't imply 'import site' on initialization -u : force the stdout and stderr streams to be unbuffered; this option has no effect on stdin; also PYTHONUNBUFFERED=x -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity -V : print the Python version number and exit (also --version) when given twice, print more information about the build -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg -x : skip first line of source, allowing use of non-Unix forms of #!cmd -X opt : set implementation-specific option. The following options are available: -X faulthandler: enable faulthandler -X showrefcount: output the total reference count and number of used memory blocks when the program finishes or after each statement in the interactive interpreter. This only works on debug builds -X tracemalloc: start tracing Python memory allocations using the tracemalloc module. By default, only the most recent frame is stored in a traceback of a trace. Use -X tracemalloc=NFRAME to start tracing with a traceback limit of NFRAME frames -X showalloccount: output the total count of allocated objects for each type when the program finishes. This only works when Python was built with COUNT_ALLOCS defined -X importtime: show how long each import takes. It shows module name, cumulative time (including nested imports) and self time (excluding nested imports). Note that its output may be broken in multi-threaded application. Typical usage is python3 -X importtime -c 'import asyncio' -X dev: enable CPython's "development mode", introducing additional runtime checks which are too expensive to be enabled by default. Effect of the developer mode: * Add default warning filter, as -W default * Install debug hooks on memory allocators: see the PyMem_SetupDebugHooks() C function * Enable the faulthandler module to dump the Python traceback on a crash * Enable asyncio debug mode * Set the dev_mode attribute of sys.flags to True * io.IOBase destructor logs close() exceptions -X utf8: enable UTF-8 mode for operating system interfaces, overriding the default locale-aware mode. -X utf8=0 explicitly disables UTF-8 mode (even when it would otherwise activate automatically) -X pycache_prefix=PATH: enable writing .pyc files to a parallel tree rooted at the given directory instead of to the code tree --check-hash-based-pycs always|default|never: control how Python invalidates hash-based .pyc files file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:] Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) PYTHONPATH : ':'-separated list of directories prefixed to the default module search path. The result is sys.path. PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>). The default module search path uses <prefix>/lib/pythonX.X. PYTHONCASEOK : ignore case in 'import' statements (Windows). PYTHONUTF8: if set to 1, enable the UTF-8 mode. PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr. PYTHONFAULTHANDLER: dump the Python traceback on fatal errors. PYTHONHASHSEED: if this variable is set to 'random', a random value is used to seed the hashes of str and bytes objects. It can also be set to an integer in the range [0,4294967295] to get hash values with a predictable seed. PYTHONMALLOC: set the Python memory allocators and/or install debug hooks on Python memory allocators. Use PYTHONMALLOC=debug to install debug hooks. PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of locale coercion and locale compatibility warnings on stderr. PYTHONBREAKPOINT: if this variable is set to 0, it disables the default debugger. It can be set to the callable of your debugger of choice. PYTHONDEVMODE: enable the development mode. PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.
5: 新的需求: 怎么在命令行中禁用断言,看下面的对比代码:
最关键是修改__debug__的值
(sse38) lzh mac$ python Python 3.8.13 (default, Mar 28 2022, 06:16:26) [Clang 12.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> __debug__ True >>> exit() (sse38) lzh mac$ python -O Python 3.8.13 (default, Mar 28 2022, 06:16:26) [Clang 12.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> __debug__ False >>>
这部分是在命令行中操作的
mac or linux 系统中,
(sse38) mac$ export PYTHONOPTIMIZE=0 (sse38) mac$ python demo.py True (sse38) mac$ export PYTHONOPTIMIZE=1 (sse38) mac$ python demo.py False (sse38) mac$ export PYTHONOPTIMIZE=2 (sse38) mac$ python demo.py False
window 系统中,
(sse38) mac$ set PYTHONOPTIMIZE=0 (sse38) mac$ python demo.py True (sse38) mac$ set PYTHONOPTIMIZE=1 (sse38) mac$ python demo.py False (sse38) mac$ set PYTHONOPTIMIZE=2 (sse38) mac$ python demo.py False
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文主要为大家介绍了OpenCV中一些简单的绘图函数的使用教程,文中的示例代码讲解详细,对我们了解OpenCV有一定的帮助,感兴趣的可以学习一下
这篇文章主要介绍了上手简单,功能强大的Python爬虫框架――feapder的使用教程,帮助大家更好的利用python进行爬虫,感兴趣的朋友可以了解下
这篇文章主要为大家介绍了Python内建属性getattribute拦截器使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要介绍了如何利用Python语言实现水波特效,文中的示例代码讲解详细,对我们学习Python有一定的帮助,需要的可以参考一下
python如何求两个向量夹角?如下图所示我们想要求两个向量的顺时针夹角,那么我们要怎么样做呢?有什么方法?这篇文件就给大家介绍 python如何计算两向量的顺时针夹角,感兴趣的朋友就继续看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008