str() of a tuple
Mark McEahern
marklists at mceahern.com
Wed Sep 4 17:38:56 EDT 2002
> I'm sure this is a FAQ[...]
It is:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=slrnacjga3.2uv.huaiy
u%40gauss.almadan.ibm.com
> Is there a convenient way to print out a tuple of floats so that the
> output is actually readable?
Subclass tuple and do your own __str__:
$ python
>>> class mytuple(tuple):
... def __repr__(self):
... strings = [str(x) for x in self]
... s = ','.join(strings)
... return "(%s)" % s
...
>>> t = mytuple((2, 0.4))
>>> t
(2,0.4)
>>> print (2, 0.4)
(2, 0.40000000000000002)
// m
-
More information about the Python-list
mailing list