Python tabnanny 模块
实例
检查 Python 源代码是否存在缩进歧义:
import tabnanny
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write('def hello():
print("Hello")
')
fname = f.name
tabnanny.check(fname)
print('Indentation check completed')
亲自试一试 »
定义和用法
tabnanny 模块可以检测 Python 源文件中的缩进歧义。
使用它来检查制表符和空格的混用是否一致,这可能会导致 Python 代码中出现一些不易察觉的错误。
成员
| 成员 | 描述 |
|---|---|
| NannyNag | 检测到缩进不明确时引发异常。 |
| check() | 检查文件是否存在缩进不明确的情况。 |
更多示例
实例
检测代码中的缩进歧义:
import tabnanny
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write('def greet():
print("Hello")
print("World")
')
fname = f.name
try:
tabnanny.check(fname)
print('No indentation errors found')
except tabnanny.NannyNag as e:
print(f'Indentation error: {e}')
亲自试一试 »

