我有一个Vector
课程如下:
class Vector(object):
def __init__(self, coordinates):
self.coordinates = tuple([Decimal(x) for x in coordinates])
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
如果我运行以下代码......
v1 = Vector([1,1])
print v1
......我得到了
Vector: (Decimal('1'), Decimal('1'))
如何摆脱标签'十进制'? 输出应如下所示:
Vector: (1, 1)
答案 0 :(得分:1)
只需拨打str
功能:
import decimal
d = decimal.Decimal(10)
d
Decimal('10')
str(d)
'10'
代码:
def __str__(self):
return 'Vector: {}'.format(map(str, self.coordinates))
答案 1 :(得分:1)
在小数点周围添加str()
:
from __future__ import print_function
from decimal import Decimal
class Vector(object):
def __init__(self, coordinates):
self.coordinates = tuple([Decimal(x) for x in coordinates])
def __str__(self):
return 'Vector: ({})'.format(', '.join(str(x) for x in self.coordinates))
v1 = Vector([1,1])
print(v1)
输出:
Vector: (1, 1)