Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1 view
Skip to first unread message

Karthik Gurusamy

unread,
Jul 16, 2007, 8:10:07 PM7/16/07
to
Hi,

The string format operator, %, provides a functionality similar to the
snprintf function in C. In C, the function does not know the type of
each of the argument and hence relies on the embedded %<char>
specifier to guide itself while retrieving args.

In python, the language already provides ways to know the type of an
object.

So in

output = '%d foo %d bar" % (foo_count, bar_count),
why we need to use %d? I'm thinking some general common placeholder,
say %x (currently it's hex..) could be used.

output = '%x foo %x bar" % (foo_count, bar_count).
Since % by definition is string formatting, the operator should be
able to infer how to convert each of the argument into strings.

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.

Thanks,
Karthik

Dan Bishop

unread,
Jul 16, 2007, 8:18:32 PM7/16/07
to
On Jul 16, 7:10 pm, Karthik Gurusamy <kar1...@gmail.com> wrote:
> Hi,
>
> The string format operator, %, provides a functionality similar to the
> snprintf function in C. In C, the function does not know the type of
> each of the argument and hence relies on the embedded %<char>
> specifier to guide itself while retrieving args.
>
> In python, the language already provides ways to know the type of an
> object.
>
> So in
>
> output = '%d foo %d bar" % (foo_count, bar_count),
> why we need to use %d?
In order to distinguish between, for example:

>>> '%c' % 42
'*'
>>> '%d' % 42
'42'
>>> '%e' % 42
'4.200000e+01'
>>> '%f' % 42
'42.000000'
>>> '%g' % 42
'42'
>>> '%i' % 42
'42'
>>> '%o' % 42
'52'
>>> '%r' % 42
'42'
>>> '%s' % 42
'42'
>>> '%u' % 42
'42'
>>> '%x' % 42
'2a'

mensa...@aol.com

unread,
Jul 16, 2007, 8:23:37 PM7/16/07
to
On Jul 16, 7:10 pm, Karthik Gurusamy <kar1...@gmail.com> wrote:
> Hi,
>
> The string format operator, %, provides a functionality similar to the
> snprintf function in C. In C, the function does not know the type of
> each of the argument and hence relies on the embedded %<char>
> specifier to guide itself while retrieving args.
>
> In python, the language already provides ways to know the type of an
> object.
>
> So in
>
> output = '%d foo %d bar" % (foo_count, bar_count),
> why we need to use %d? I'm thinking some general common placeholder,
> say %x (currently it's hex..) could be used.
>
> output = '%x foo %x bar" % (foo_count, bar_count).
> Since % by definition is string formatting, the operator should be
> able to infer how to convert each of the argument into strings.

You want all your numbers to print in hexadecimal?

>
> 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.

Who does that?

>
> Thanks,
> Karthik


Matimus

unread,
Jul 16, 2007, 8:30:58 PM7/16/07
to
I don't have a good answer for you, but you might be interested to
read this: http://python.org/dev/peps/pep-3101/. Which according to a
recent blog post by BDFL is going to be how string formatting is done
in Python3000.

The character doesn't specify the type to expect, but the formatting
function. So, %s calls a string formatter, %r calls repr and %x calls
a hex formatter. The there may be multiple formatters that produce
different results for given types. An integer can use %d,%e,%f,%s,%x
or %r, and they all produce slightly different results. Also, the
formatters take parameters. Such as "%+010.5f"%(1.23) which produces
"+001.23000".


Karthik Gurusamy

unread,
Jul 16, 2007, 8:33:53 PM7/16/07
to

Thanks. The above surprised me as I didn't expect that %s will accept
42.

Looks like the implicit conversion doesn't work the other way.

>>> '%s' % 42
'42'
>>> '%d' % '42'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int argument required
>>>

Looks like %s can be used even when I'm sending non-strings.
>>> '%s foo %s bar' % (25, 25.34)
'25 foo 25.34 bar'
>>>

So %s seems to serve the multi-type placeholder.

Karthik

marduk

unread,
Jul 16, 2007, 9:06:27 PM7/16/07
to
On Mon, 2007-07-16 at 17:33 -0700, Karthik Gurusamy wrote:
> Thanks. The above surprised me as I didn't expect that %s will accept
> 42.
>
> Looks like the implicit conversion doesn't work the other way.
>
> >>> '%s' % 42
> '42'
> >>> '%d' % '42'
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: int argument required
> >>>
>
> Looks like %s can be used even when I'm sending non-strings.
> >>> '%s foo %s bar' % (25, 25.34)
> '25 foo 25.34 bar'
> >>>
>
> So %s seems to serve the multi-type placeholder.

According to the docs: http://docs.python.org/lib/typesseq-strings.html

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


Miles

unread,
Jul 16, 2007, 10:03:16 PM7/16/07
to
On Jul 16, 8:10 pm, Karthik Gurusamy <kar1...@gmail.com> wrote:
> Since % by definition is string formatting, the operator should be
> able to infer how to convert each of the argument into strings.

In addition to what Dan mentioned, you can use "%s" with any object to
perform an automatic string conversion.

>>> '%s %s %s %s' % ('Hello!', 3.14, 42+1j, object())
'Hello! 3.14 (42+1j) <object object at 0x41448>'

-Miles

Kay Schluehr

unread,
Jul 17, 2007, 12:51:47 AM7/17/07
to
On Jul 17, 3:10 am, Karthik Gurusamy <kar1...@gmail.com> wrote:

> output = '%d foo %d bar" % (foo_count, bar_count),
> why we need to use %d? I'm thinking some general common placeholder,
> say %x (currently it's hex..) could be used.

You already answered it in the parenthesized remark: the %d
placeholder is not only type bound but provides an additonal
distinction e.g. the one between decimals and hexadecimals. The kind
of general placeholder you want %x being requested for is actually %s
which formats decimals quite well unless you want leading zeros.

Paddy

unread,
Jul 17, 2007, 2:19:41 AM7/17/07
to

'%s' might be what your after as a more 'general purpose' moifier.

- Paddy.

Asun Friere

unread,
Jul 17, 2007, 2:22:34 AM7/17/07
to
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.

Duncan Booth

unread,
Jul 17, 2007, 3:38:05 AM7/17/07
to
marduk <mar...@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'

star....@gmail.com

unread,
Jul 17, 2007, 12:46:37 PM7/17/07
to
> - Paddy.- Hide quoted text -
>
> - Show quoted text -

It is good for that; I generally use %s until I decide that something
needs picky formatting.

--
a = '%s Weaver' % random.choice(['Lani','Star','Azure'])
a += 'is strange.'

Asun Friere

unread,
Jul 17, 2007, 10:25:39 PM7/17/07
to
On Jul 17, 5:38 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:

> indeed anything which has an __int__ method may be
> passed to the %d formatter:

Anything?! Sorry to be persnickety here, but what about this:

class C :
def __int__ (self) : pass

'%d' % C()

or this:

def foo (val) : return val
foo.__int__ = lambda x=42 : int(x)

'%d' % foo('spam')

OK, they can be passed ...

Duncan Booth

unread,
Jul 18, 2007, 3:39:39 AM7/18/07
to
Asun Friere <afr...@yahoo.co.uk> wrote:

> On Jul 17, 5:38 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
>
>> indeed anything which has an __int__ method may be
>> passed to the %d formatter:
>
> Anything?! Sorry to be persnickety here, but what about this:
>
> class C :
> def __int__ (self) : pass
>
> '%d' % C()
>

__int__ is a reserved method name so it is reasonable to assume that any
such method will act as documented: "Should return a value of the
appropriate type." If you choose to ignore the documentation then expect
exceptions to be thrown.

> or this:
>
> def foo (val) : return val
> foo.__int__ = lambda x=42 : int(x)
>
> '%d' % foo('spam')
>

I don't see any methods named '__int__' there, just a function assigned to
an attribute. (Yes, I know that isn't the problem, assigning a function to
an __int__ attribute works for other types, but if you can be silly so can
I.)

0 new messages