473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

format sring question

In Peter Norvig's Infrequently Answered Questions he explains that the
following 2 fnctions look almost identical but are not the same:

def printf(format, *args): print format % args,

def printf(format, *args): print str(format) % args,

The only difference is that in the second one, str(format) replaces
format. If args are not given and the format string contains a '%', the
first will work but the second will not. Why is this so? It seems to me
like '100%' and str('100%) are the same object, no?

Lowell
Jul 18 '05 #1
3 2144
Lowell Kirsh wrote:
In Peter Norvig's Infrequently Answered Questions he explains that the
following 2 fnctions look almost identical but are not the same:

def printf(format, *args): print format % args,

def printf(format, *args): print str(format) % args,

The only difference is that in the second one, str(format) replaces
format. If args are not given and the format string contains a '%', the
first will work but the second will not. Why is this so? It seems to me
like '100%' and str('100%) are the same object, no?


The both fail the same way for me; I can't reproduce
a difference.
def printf1(format, *args): print format % args, .... def printf2(format, *args): print str(format) % args, .... printf1("100%") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in printf1
ValueError: incomplete format printf2("100%") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in printf2
ValueError: incomplete format printf1("100%%") 100% printf2("100%%") 100%


My guess is there's supposed to be some sort of
confusing between the trailing ',' when used in a
print statement, to suppress the trailing newline,
and when used to indicate a tuple, as in

a = args,

to create a single element tuple. In that case,
% args, might be a single element tuple where the
first element is the tuple of args.

Andrew
da***@dalkescientific.com
Jul 18 '05 #2
Lowell Kirsh wrote:
In Peter Norvig's Infrequently Answered Questions he explains that the
following 2 fnctions look almost identical but are not the same:

def printf(format, *args): print format % args,

def printf(format, *args): print str(format) % args,

The only difference is that in the second one, str(format) replaces
format. If args are not given and the format string contains a '%', the
first will work but the second will not. Why is this so? It seems to me
like '100%' and str('100%) are the same object, no?

Lowell


The only difference is that the _second_ one will work for non-strings, too,
when only the 'format' argument is passed and str(format) does not contain
any '%' characters. Two consecutive '%' are always converted into one, I'd
say this is broken behaviour when the conversion is applied on the result
of str(format) and format is not a string.

IMHO the second form is extremely bad code and you better forget about it
fast.

Peter

Jul 18 '05 #3
thanks to both of you. bye bye second version.

Lowell

Lowell Kirsh wrote:
In Peter Norvig's Infrequently Answered Questions he explains that the
following 2 fnctions look almost identical but are not the same:

def printf(format, *args): print format % args,

def printf(format, *args): print str(format) % args,

The only difference is that in the second one, str(format) replaces
format. If args are not given and the format string contains a '%', the
first will work but the second will not. Why is this so? It seems to me
like '100%' and str('100%) are the same object, no?

Lowell

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
1
by: Tom | last post by:
In the Developers Handbook, Getz provides a way to create a survey report that displays either a line, Yes/No or multiple choice for each question. It's done by placing three controls (one for each...
2
by: Rock | last post by:
Hi,all: how to convert VBA #date# format (ex: #2004/3/6#) to C# code ? here are vba code : Dim objUserPerm As Office.UserPermission Set objUserPerm =...
16
by: Al Reid | last post by:
First, I'm using vb2005. I have a string that is read from a barcode reader into a TextBox. The string is 6 characters long and represents a date (mmddyy). I want to display it to the user in a...
2
by: gjuro kladaric | last post by:
hi there, I am just learning about IFormattable, ICustomFormatter, IFormatProvider and the like... although being a bit tricky to understand, it becomes clear after some clicking and...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
10
by: ARC | last post by:
Hello all, General question for back-end database that has numerous date fields where the database will be used in regions that put the month first, and regions that do not. Should I save a...
12
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there...
9
by: Martin | last post by:
I'm retrieving some records from a database. One of the fields contains a date/time. I would like to format it as I send it out to the table in the displayed page. Can some one please tell me...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.