Bug in __imul__

Skip Montanaro skip at pobox.com
Wed Jul 11 11:23:30 EDT 2001


    Emile> Does it bother anyone that 
    Emile> a *= 3 + 4
    Emile> returns a different value from
    Emile> a = a * 3 + 4
    Emile> ??

No more than it bothers me that C behaves the same way.  When run, the
following program

    main() {
        int a;
        
        a = 7;
        printf("before: %d\n", a);
        a *= 3+4;
        printf("after (no parens): %d\n", a);
        
        a = 7;
        printf("before: %d\n", a);
        a *= (3+4);
        printf("after (w/ parens): %d\n", a);
        
        a = 7;
        printf("before: %d\n", a);
        a = a * 3 + 4;
        printf("after (unaugmented): %d\n", a);
    }

produces the following output

    before: 7
    after (no parens): 49
    before: 7
    after (w/ parens): 49
    before: 7
    after (unaugmented): 25

In short, the effect of the shortcut assignment operators is to evaluate the
expression on the right-hand side and apply it to the variable on the
left-hand side.  It is *not* a macro facility that just performs some sort
of textual substitution.

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list