+= as declaration wish

Tim Peters tim.one at home.com
Thu Feb 15 22:39:57 EST 2001


[Gregory (Grisha) Trubetskoy]
> 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.

Only that it's semantically confused, so Guido will never buy it.

[Erik Max Francis]
> Avoiding such magic like this is what makes Python a well-designed
> language.  If you want x to default to something, you can set it to that
> yourself before you start using +=.  (Besides, what should it default to
> for ints? strings? lists? user-defined types?)

That's not a problem.  Gregory didn't ask for a default:  he wants

   LHS op= RHS

to act like

try:
    LHS
except NameError:
    LHS = RHS
else:
    LHS op= RHS

No defaults are involved.  So it's not an intractable idea.  By analogy, we
could also make f(x) act like

try:
    x
except NameError:
    f()
else:
    f(x)

and so on and so on.  Then

    x += f(y, z)

gets *really* cute <wink>.

if-a-thing's-a-rotten-idea-in-the-large-it's-probably-not-a-great-
    idea-in-the-small-ly y'rs  - tim





More information about the Python-list mailing list