Augmented assignment (ie. += and friends) for Python!

Thomas Wouters thomas at xs4all.net
Sun May 14 05:18:39 EDT 2000


On Sat, May 13, 2000 at 09:36:28PM +0000, Courageous wrote:

> > No, he didn't, because he knows they're spelt "&=" and "|=" and he
> > supports them.  Maybe you should check these things before posting?

> Possibly. Puzzled, however, what someone could possibly mean by &&=?

In Python, and Perl, the following idiom is quite oft seen:

val = val or "<null>"

(Actually, the perl version reads:

val = val || "<null>";

because assignment binds more tightly than the perl 'or' and 'and')

This works because the logical operators do not return a 'truth' value, but
instead one of their operands. (See the fine manual for an explanation ;)

Perl supplies the &&= and ||= operators as shortcuts for the above
construct. Note that Python uses 'and' and 'or' and *not* && and ||, whereas
perl uses them both, but with different precedence. As pointed out two times
already, having &&=/||= in Python would be plain silly and useless, as well
as confusing (since python doesn't have &&/|| to begin with ;)

fork-and-exit;-ly y'rs,
-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list