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

Discussion in 'Python' started by David Cournapeau, Dec 30, 2008.

  1. 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
    David Cournapeau, Dec 30, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bad_Kid
    Replies:
    3
    Views:
    3,943
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,944
    Kevin Spencer
    Apr 20, 2005
  3. EDom
    Replies:
    0
    Views:
    439
  4. Arun Kumar Srinivasan

    Using javax.print classes to print text/file

    Arun Kumar Srinivasan, Jan 13, 2004, in forum: Java
    Replies:
    5
    Views:
    6,142
    Gregory A. Swarthout
    Jan 14, 2004
  5. keto
    Replies:
    0
    Views:
    924
Loading...

Share This Page