Talin <talin <at> acm.org> writes: > 2) A suggestion which I've seen others bring up before is the use of > the * operator for tuple packing / unpacking operations, i.e.: > > a, *b = (1, 2, 3)
I wanted to add another case that I run across in my code a lot. I often times want to split off a single leading word or token from a string, but I don't know in advance whether or not the split will actually succeed. For example, suppose you want to render the first word of a paragraph in a different style: first, rest = paragraph.split( ' ', 1 ) Unfortunately, if the paragraph only contains a single word, this blows up. So what you end up having to do is: parts = paragraph.split( ' ', 1 ) if len( parts ) > 1: first, rest = parts else: first = parts[ 0 ] rest = "" My objection here is that the intent of the code is cluttered up by the error-handling logic. If I could do an argument-style unpack, however, I could instead write: first, *rest = paragraph.split( ' ', 1 ) It does mean that 'rest' is wrapped in a tuple, but that's not a terrible hardship. If there's no second word, then 'rest' is an empty tuple, which is easy to test with "if rest" and such. -- Talin _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com