471,348 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

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

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

Jul 17 '07 #1
9 998
On Jul 16, 7:10 pm, Karthik Gurusamy <kar1...@gmail.comwrote:
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

Jul 17 '07 #2
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".
Jul 17 '07 #3
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().
Jul 17 '07 #4
On Jul 16, 8:10 pm, Karthik Gurusamy <kar1...@gmail.comwrote:
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

Jul 17 '07 #5
On Jul 17, 3:10 am, Karthik Gurusamy <kar1...@gmail.comwrote:
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.

Jul 17 '07 #6
On Jul 17, 1:10 am, Karthik Gurusamy <kar1...@gmail.comwrote:
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
'%s' might be what your after as a more 'general purpose' moifier.

- Paddy.

Jul 17 '07 #7
marduk <ma****@nbk.hopto.orgwrote:
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'
Jul 17 '07 #8
On Jul 17, 2:19 am, Paddy <paddy3...@googlemail.comwrote:
On Jul 17, 1:10 am, Karthik Gurusamy <kar1...@gmail.comwrote:


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

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

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

Jul 17 '07 #9
On Jul 17, 5:38 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
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 ...

Jul 18 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephen Gennard | last post: by
3 posts views Thread by sferriol | last post: by
10 posts views Thread by jflash | last post: by
26 posts views Thread by Jerim79 | last post: by
5 posts views Thread by bearophileHUGS | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.