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

Duncan Booth duncan.booth at invalid.invalid
Tue Jul 17 03:38:05 EDT 2007


marduk <marduk at nbk.hopto.org> wrote:

> By design, %s "converts any python object using str()".  OTOH it does
> not specify that %d, for example, calls int().

No, but it does say that the 'd' is a conversion type meaning 'signed 
integer decimal', and indeed anything which has an __int__ method may be 
passed to the %d formatter:

>>> class C:
	def __int__(self):
		return 42

	
>>> "%d" % C()
'42'
>>> "%d" % 3.5
'3'



More information about the Python-list mailing list