python - 我可以在断言消息中引入变量的输出吗
问题描述
我正在对我创建的一个程序的输入数据进行一些验证。我正在使用断言来执行此操作。如果出现断言,我想知道数据的哪一部分出现,所以我想获得出现断言的值。
assert all(isinstance(e, int)
for l1 in sequence.values()
for l2 in l1 for e in l2),"Values of the dictionnary aren't lists of integers. Assert found in '{l2}'"
# This code doesn't work
解决方案
使用时不会,all
因为它不会公开“迭代变量”。您将需要一个显式的嵌套循环。您还忘记了f''
表示 f 字符串的前缀:
for l1 in [['a']]:
for l2 in l1:
for e in l2:
assert isinstance(e, int), f"Values of the dictionnary
aren't lists of integers. Assert found in '{l2}'"
AssertionError: Values of the dictionnary aren't lists of integers. Assert found in 'a'
推荐阅读
- vue.js - 使用 asyncData 进行 Nuxt 分页
- c++ - 激活标签时的鼠标指针位置
- css - 在 Bootstrap 5 中更改手风琴按钮折叠图标颜色
- java - 如何在 Spring Data 存储库中获取默认方法以命中缓存?
- c++ - C5002 1204 自动矢量化原因码是什么意思?
- mysql - 休眠不获取非拥有实体
- python - 如何将 Shopify API json 转换为可读的数据框?
- perl - 如何测试模块是否与 Perl 编译器 (perlcc) 兼容?
- node.js - 在aws中部署node js的方式有哪些?
- selenium - Selenium-side-runner 从 cmd 运行特定的 chrome 配置文件