小编jac*_*ack的帖子

在Python中,如何在脚本而不是解释器中使用十进制模块?

我正在使用Python 2.5.4并尝试使用十进制模块.当我在翻译中使用它时,我没有问题.例如,这有效:

>>> from decimal import *
>>> Decimal('1.2')+ Decimal('2.3')
Decimal("3.5")
Run Code Online (Sandbox Code Playgroud)

但是,当我把以下代码:

from decimal import *
print Decimal('1.2')+Decimal('2.3')
Run Code Online (Sandbox Code Playgroud)

在一个单独的文件(称为decimal.py)并将其作为模块运行,解释器抱怨:

NameError:未定义名称"Decimal"

我也尝试将此代码放在一个单独的文件中:

import decimal
print decimal.Decimal('1.2')+decimal.Decimal('2.3') 
Run Code Online (Sandbox Code Playgroud)

当我将其作为模块运行时,解释器说:

AttributeError:'module'对象没有属性'Decimal'

这是怎么回事?

python decimal

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

decimal ×1

python ×1