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

K

Karthik Gurusamy

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
 
D

Dan Bishop

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:
'2a'
 
M

mensanator

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?
 
M

Matimus

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

Karthik Gurusamy

In order to distinguish between, for example:



'42'

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.
Traceback (most recent call last):

Looks like %s can be used even when I'm sending non-strings.
So %s seems to serve the multi-type placeholder.

Karthik
 
M

marduk

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.

Traceback (most recent call last):


Looks like %s can be used even when I'm sending non-strings.

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().
 
M

Miles

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.
'Hello! 3.14 (42+1j) <object object at 0x41448>'

-Miles
 
K

Kay Schluehr

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

Paddy

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

Asun Friere

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

Duncan Booth

marduk said:
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:
def __int__(self):
return 42

'3'
 
S

star.public

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

Asun Friere

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

Duncan Booth

Asun Friere said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top