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 9 1031
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
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".
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().
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
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.
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.
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'
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.'
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 ... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephen Gennard |
last post by:
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example...
|
by: sferriol |
last post by:
hello
is it possible with postgres 7.2 or more, to define a dynamic view.
For example, i have a table with a column 'user'
and i want to define a view which gives infomrations from different...
|
by: serge |
last post by:
How can I run a single SP by asking multiple sales question either
by using the logical operator AND for all the questions; or using
the logical operator OR for all the questions. So it's always...
|
by: Kapil Jain |
last post by:
Dear All,
What i need to achieve is :
I am generating dynamic text boxes thru dhtml coding, i need onChange
event of oragnistation text box i.e dynamically generated on click of
"More" button in...
|
by: jflash |
last post by:
Hello all,
I feel dumb having to ask this question in the first place, but I just
can not figure it out. I am wanting to set my site up using dynamic
urls (I'm assuming that's what they're...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: Jerim79 |
last post by:
I need to create a form that takes a number that the user enters, and
duplicates a question the number of times the user entered. For
instance, if the customer enters 5 on the first page, when...
|
by: bearophileHUGS |
last post by:
I often use Python to write small programs, in the range of 50-500
lines of code. For example to process some bioinformatics data,
perform some data munging, to apply a randomized optimization...
|
by: remya1000 |
last post by:
i'm using VB.net 2003 application program. i'm trying to convert a VB6 program to VB.NET. The VB6 code i'm trying to convert is shown below.
declared g_Share() array in module and trying to add...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| | |