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

D

David Cournapeau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top