473,811 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2163
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***@dalkescie ntific.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
31190
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 format exception (SQLSTATE=22007). The same queries worked fine on all the other workstations. The date format we want to use is "dd/mm/yyyy".After reinstalling several times the db2 runtime client w/ different options, we found on a technical forum...
1
5774
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 type of answer) in the report's detail section and then making the control appropriate for each question visible in the Format event of the detail section. Can this be simulated in a form? There's no Detail Format event for a form but can it be...
2
4870
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 = ActiveWorkbook.Permission.Add("user@domain.com", msoPermissionRead + msoPermissionEdit, #12/31/2005#) a to c# ?? I don't know how to convert this code to C#? and I have already try it use DateTime format, but it happen argument error. anyone have the same problem ? ...
16
4025
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 date format of "mm/dd/yy" For example the barcode contains "112303" and I want to format it to display "11/23/03" If I use the microsoft.visualbasic.strings.format with a format string of "##/##/##" or "00/00/00" I get the format string in the...
2
1575
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 readying... what is not clear to me and constitutes my question is:
13
3306
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 to configure server, sql server or program so it always deals with date as mm/dd/yyyy format. Thanks in advance. Roy
10
5827
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 date format in the table design, such as: mm/dd/yyyy? What I've done for years is to store the date format in date fields, then on the forms, based on their region, I would set the date formats on form_load
12
4486
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 a way to get the format of the %x directive (it differs across locales)? For ex, default (locale is C), %x gives like %m/%d/%y. Thank you
9
2943
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 how I can/should do that? Or possibly point me to an on-line explanation? Thanks ps: if it makes any difference, the data is coming from a MS Access
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.