Augmented Assignment question

Discussion in 'Python' started by Doug Tolton, Jul 16, 2003.

  1. Doug Tolton

    Doug Tolton Guest

    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
    Doug Tolton, Jul 16, 2003
    #1
    1. Advertising

  2. Doug Tolton

    Adam Ruth Guest

    In <> Doug Tolton wrote:
    > 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
    >


    I don't think it's all that inelegant, and it's definitely clear. I can
    see, though, why a one liner would seem a little cleaner.

    Off the top of my head, I'd say to change your checkdoc function to this:

    > def checkdoc(self, document, normal=0, truncated=0, semicolon=0):
    > blen = document['length']
    > bates = document['obates']
    > 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


    And then call it like this:

    > normal, truncated, semicolon = self.checkdoc(curdoc, normal,
    > truncated, semicolon)


    As a side note, I wouldn't have thought that the augmented assign would
    work the way you tried to use it. I would have thought that it would be
    analagous to the + operator and sequences:

    > x = [1,2,3]
    > y = [1,2,3]
    > x + y

    [1,2,3,1,2,3]

    So that the agumented form would be:

    > x = [1,2,3]
    > x += [1,2,3]

    [1,2,3,1,2,3]

    But I've never tried it before and didn't know that it didn't work with
    sequences. You learn something new every day.

    Adam Ruth
    Adam Ruth, Jul 16, 2003
    #2
    1. Advertising

  3. Doug Tolton

    Aahz Guest

    In article <>,
    Doug Tolton <> wrote:
    >
    >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?


    Really?

    >>> l = []
    >>> l+=[1]
    >>> l

    [1]
    >>> l+=['foo']
    >>> l

    [1, 'foo']
    --
    Aahz () <*> http://www.pythoncraft.com/

    A: No.
    Q: Is top-posting okay?
    Aahz, Jul 16, 2003
    #3
  4. Quoth Doug Tolton:
    [...]
    > 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?


    It seems possible that your normal, truncated and semicolon
    variables should be bundled into a single object:

    class DocumentStatistics(object):
    # ...
    def __iadd__(self, (deltanormal, deltatruncated, deltasemicolon)):
    self.normal += deltanormal
    self.truncated += deltatruncated
    self.semicolon += deltasemicolon

    Then you can just do
    docstats = DocumentStatistics()
    # ...
    docstats += self.checkdoc(curdoc)
    If these variables usually change in synch, this seems a natural
    way to organize them.

    You might also want to look into Numeric's arrays.

    (And there's always

    normal, truncated, semicolon = map(operator.add,
    (normal, truncated, semicolon),
    self.checkdoc(curdoc))

    which I consider inferior to your straightforward approach with
    three augmented assignments.)

    --
    Steven Taschuk
    "Please don't damage the horticulturalist."
    -- _Little Shop of Horrors_ (1960)
    Steven Taschuk, Jul 17, 2003
    #4
  5. Doug Tolton

    Tim Lesher Guest

    Doug Tolton <> wrote in message news:<>...
    > This solution strikes me as inelegant and ugly. Is there a cleaner
    > way of accomplishing this?


    The problem is this: performing addition of tuples is concatenation,
    not addition of the elements. If you try to decompose the augmented
    addition into an addition and an assignment, you get a tuple that is
    (in your case) the concatenation of the two 3-tuples, or a 6-tuple,
    which can't unpack into a 3-tuple.

    That said, for some definition of "cleaner", you could do:
    def accumulate(i,j):
    return map(int.__add__, i, j)

    // loop
    i,j,k = accumulate((i,j,k), fn())
    Tim Lesher, Jul 17, 2003
    #5
  6. On Wed, 16 Jul 2003 22:17:40 GMT, Doug Tolton <> wrote:

    >On 16 Jul 2003 16:51:38 -0400, (Aahz) wrote:
    >
    >>In article <>,
    >>Doug Tolton <> wrote:
    >>>
    >>>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?

    >>
    >>Really?
    >>
    >>>>> l = []
    >>>>> l+=[1]
    >>>>> l

    >>[1]
    >>>>> l+=['foo']
    >>>>> l

    >>[1, 'foo']

    >
    >
    >I mis-spoke, lists are not included. You cannot do augmented
    >assignments on tuples or multiple targets.
    >
    >you can do what you typed, but you can't do.
    >
    >>>> a,b = 0,0
    >>>> a,b += 1,1

    >SyntaxError: augmented assign to tuple not possible
    >
    >or this:
    >
    >>>> a,b = [],[]
    >>>> a,b += [1],[1]

    >SyntaxError: augmented assign to tuple not possible
    >
    >That is specifically the point I was getting at. Forgive the
    >technical slip of including lists in the discussion.
    >

    A little experiment (not tested beyond what you see ;-):

    >>> class TX(tuple):

    ... def __add__(self, other):
    ... return TX([s+o for s,o in zip(self, other)])
    ...
    >>> tx = TX((2,2))
    >>> tx

    (2, 2)
    >>> tx + (3,5)

    (5, 7)
    >>> tx

    (2, 2)
    >>> tx += (3,5)
    >>> tx

    (5, 7)
    >>> tx += 10,20
    >>> tx

    (15, 27)
    >>> tt = TX(('ab','cd','ef'))
    >>> tt

    ('ab', 'cd', 'ef')
    >>> tt += '123'
    >>> tt

    ('ab1', 'cd2', 'ef3')


    Regards,
    Bengt Richter
    Bengt Richter, Jul 17, 2003
    #6
  7. Doug Tolton

    John Machin Guest

    Doug Tolton <> wrote in message news:<>...
    > 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


    self.total_normal += normal
    # etc etc
    # someone else suggested a separate class just for statistics
    # --- this is overkill IMHO
    >
    > return normal, truncated, semicolon
    >
    John Machin, Jul 19, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Delaney, Timothy C (Timothy)

    RE: Augmented Assignment question

    Delaney, Timothy C (Timothy), Jul 17, 2003, in forum: Python
    Replies:
    0
    Views:
    379
    Delaney, Timothy C (Timothy)
    Jul 17, 2003
  2. Pierre Barbier de Reuille

    User-defined augmented assignment

    Pierre Barbier de Reuille, Sep 29, 2005, in forum: Python
    Replies:
    4
    Views:
    302
    Tom Anderson
    Oct 2, 2005
  3. Suresh Jeevanandam

    Augmented assignment

    Suresh Jeevanandam, Feb 21, 2006, in forum: Python
    Replies:
    8
    Views:
    330
    Terry Hancock
    Feb 21, 2006
  4. Josiah Carlson
    Replies:
    4
    Views:
    348
    Boris Borcic
    Jun 19, 2006
  5. Tim N. van der Leeuw

    Nested scopes, and augmented assignment

    Tim N. van der Leeuw, Jul 4, 2006, in forum: Python
    Replies:
    39
    Views:
    785
    Piet van Oostrum
    Jul 10, 2006
Loading...

Share This Page