Tuple "detection"

Alexander Semenov sav at ulj.menatepspb.com
Mon May 19 16:51:27 EDT 2003


Hi

 >>>> print "%d %d" % 5, 5
 SM>     Traceback (most recent call last):
 SM>       File "<stdin>", line 1, in ?
 SM>     TypeError: not enough arguments for format string

 SM>     Facundo> Is this on purpose? Why?

 SM> Yes.  It's because the '%' operator has higher precedence than the
 SM> ','
 SM> operator.  It's as if you typed

 SM>     print ("%d %d" % 5), 5

 SM> Specifying tuples without the surrounding parentheses can sometimes
 SM> bite you.  It's generally best to always include them.

I have proposal on that. Why we use that perlish %?
(I know it's from C, but that fact can't avoid perlishness of %)
Strings are full featured objects in python,
this operation very common to strings and takes variable number of arguments
(ok, now it's variable sized tuple)

I propose change % to str.__call__()
This can clear many questions about precedence of operation and tuple
packing.
In my opinion, this looks great:
'Hello %s, you got %d apples.'('Bob', 5)
or
'Hello %(name)s, you got %(number)d apples.'(name='Bob', number=5)

This maybe done without any impact to current codebase,
just adding new method to strings.

I don't think I have ability to write corresponding pep or patch.
If somebody can do it it will be great.

With best regards,
Alexander Semenov






More information about the Python-list mailing list