【问题标题】:AttributeError: module 'math' has no attribute '_name_'AttributeError:模块“数学”没有属性“_name_”
【发布时间】: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


【解决方案1】:

__name__ 属性前后需要两个下划线。

tmpl="This {mod.__name__} module defines the value of pie as  {mod.pi}"

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2019-07-20
    • 2021-11-05
    • 2021-11-04
    相关资源
    最近更新 更多