+= as declaration wish

Brad Bollenbach bbollenbach at homenospam.com
Thu Feb 15 23:21:48 EST 2001


The reason this isn't, and shouldn't be allowed is simple. From the Python
Documentation:

"An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression list,
performs the binary operation specific to the type of assignment on the two
operands, and assigns the result to the original target."

In other words, when you do x += 1, it will evaluate x, and perform (x + 1)
before assigning back to x, which is an error in Python if x doesn't exist.

I'm curious: in what situations does this do more to help the look of your
code than not? "Working around" this should never cause any problems.

"Gregory (Grisha) Trubetskoy" <grisha at verio.net> wrote in message
news:Pine.BSF.4.32.0102151652290.71736-100000 at localhost...
>
> Am I missing something obvious when I think that it'd be nice if you could
> do:
>
> x += 3
>
> and if x didn't exist, += would simply act like = rather than raise an
> exception.
>
> Grisha
>





More information about the Python-list mailing list