print a vs print '%s' % a vs print '%f' a

David Cournapeau cournape at gmail.com
Tue Dec 30 04:19:02 CET 2008


Hi,

While working on some python C extensions, I got curious in how things
work for printing float objects (and C-level objects which inherit
from it). In python 2.6, I understand that the formatting went into
surgery for more consistency across platforms. So for example, on
windows, complex('inf') would be printed 'inf', and not '1.#INF'.
However, I would like some clarifications if possible about the
following:

a = complex('inf')
print a # -> print 'inf'
print '%s' % a # -> print 'inf'
print '%f' % a # -> print '1.#INF'

My understanding is that in the first case, the formatting is
controlled through the tp_print and in the second case, from str(a)
behavior (tp_str). Can the 3rd one be controled at all ? Or is it
going directly through the underlying C runtime, without python doing
any formatting (which is how I understand the inconsistency). Is this
by design ?

thanks,

David



More information about the Python-list mailing list