Augmented Assignment question

Doug Tolton dtolton at
Wed Jul 16 20:13:51 CEST 2003

I have a function that returns a tuple:

    def checkdoc(self, document):
        blen = document['length']
        bates = document['obates']

        normal, truncated, semicolon = 0,0,0
        for bat in bates:
            if len(bat) == 2 * blen:
                semicolon += 1
            if len(bat) == blen - 1:
                truncated += 1
            if len(bat) == blen:
                normal += 1

        return normal, truncated, semicolon

on the other side I have 3 variables:
normal, truncated and semicolon

I would like to be able to do an augmented assignment such as:

normal, truncated, semicol += self.checkdoc(mydoc)

however this returns the following error:
SyntaxError: augmented assign to tuple not possible

I did some reading and it seems that an augmented assignment is
specifically verboten on tuples and lists.  Is there a clean way to
accomplish this?  I dislike in the extreme what I've resorted to:

fnormal, ftruncated, fsemicolon = 0,0,0

// loop through a file and process all documents:
	normal, truncated, semicolon = self.checkdoc(curdoc)
	fnormal += normal
	ftruncated += truncated
	fsemicolon += semicolon

This solution strikes me as inelegant and ugly.  Is there a cleaner
way of accomplishing this?

Thanks in advance,
Doug Tolton
dougt at<remove this>case<remove this too>data dot com

More information about the Python-list mailing list