【发布时间】:2019-10-24 04:08:48
【问题描述】:
我正在尝试字符串格式 - 长字符串“替换字段名称”。但我收到“属性错误”。
我已经搜索过该错误,但没有类似的答案。
import math
tmpl="This {mod._name_} module defines the value of pie as {mod.pi}"
print(tmpl.format(mod=math))
Traceback(最近一次调用最后一次):
文件“E:/Python/Beginning Python/Ch-3/2String-Long version.py”,第 14 行,在
打印(tmpl.format(mod=math))
AttributeError: 模块 'math' 没有属性 'name'
我希望这个错误不会发生。
【问题讨论】:
-
试试
__name__,而不是_name_。
标签: python python-3.x