In a dynamic language, why % operator asks user for type info?

Asun Friere afriere at yahoo.co.uk
Tue Jul 17 08:22:34 CEST 2007


In practice the different placeholders give you a handle to achieve
different formatting effects.

Compare for example:

for x in range(15) :
  print '%02d' % x

against:

for x in range(15) :
  print '%02s' % x
  #the '0' in this case being redundant

Or again:

from math import pi
print '%.4f' % pi
print '%.4s' % pi
#if that last seems silly consider
print '%.4s % 'hello world'

Given that there already exists an established (and documented) printf
convention, one could argue that overloading (or even augmenting) the
established placeholders is not the best option in this case.

>If the above is the case, we could've avoided all those exceptions
>that happen when a %d is specified but say a string is passed.

Generally I prefer handling exceptions to avoiding them.




More information about the Python-list mailing list