Death to tuples!

Bengt Richter bokr at oz.net
Tue Nov 29 19:37:14 CET 2005


On Tue, 29 Nov 2005 09:26:53 -0600, jepler at unpythonic.net wrote:

>
>--cvVnyQ+4j833TQvp
>Content-Type: text/plain; charset=us-ascii
>Content-Disposition: inline
>
>On Tue, Nov 29, 2005 at 10:41:13AM +0000, Bengt Richter wrote:
>> Seems like str.__mod__ could take an arbitary (BTW, matching length, necessarily?
>> Or just long enough?) iterable in place of a tuple, just like it can take
>> an arbitrary mapping object in place of a dict for e.g. '%(name)s'% {'name':'<name value>'}
>
>What, and break reams of perfectly working code?
>
There you go again ;-) There I went again, d'oh ;-/

>s = set([1, 2, 3])
>t = [4, 5, 6]
>u = "qwerty"
>v = iter([None])
>print "The set is: %s" % s
>print "The list is: %s" % t
>print "The string is: %s" % u
>print "The iterable is: %s" % v

I guess I could argue for an iterable with a next method in single-arg form
just having its next method called to get successive arguments. If you
wanted the above effect for v, you would have to do the same as you now do to
print a single tuple argument, i.e., you wrap it in a 1-tuple like (v,)

This might even let you define an object that has both __getitem__ and next
methods for mixing formats like '%(first)s %s %(third)s' % map_n_next_object

reminder...

 >>> tup = (1, 2, 3)
 >>> print "The tuple is: %s" % tup
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 TypeError: not all arguments converted during string formatting
 >>> print "The tuple is: %s" % tup,
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 TypeError: not all arguments converted during string formatting
 >>> print "The tuple is: %s" % (tup,)
 The tuple is: (1, 2, 3)

But, yeah, it is handy to pass a single non-tuple arg.

But iterables as iterators have possibilities too, e.g. maybe
    it = iter(seq)
    print 'From %s: %s' % (it,)
    print 'Unpacked: %s %s' % it # as if (it.next(). it.next())
    print 'Next two: %s %s' % it # ditto

You will get StopIteration if you run out of args though, so maybe str.__mod__
should catch that and convert it to its "TypeError: not enough arguments for format string"
when/if it finally happens.

Regards,
Bengt Richter



More information about the Python-list mailing list