operator overloading + - / * = etc...

Fredrik Lundh fredrik at pythonware.com
Tue Oct 10 12:32:08 CEST 2006

Roman Neuhauser wrote:

> People who complain often fail to see how
>    x = foo()
>    while x:
>        process(x)
>        x = foo()
>    is safer than
>    while x = foo():
>        process(x)

that's spelled:

    for x in foo():

in Python, or, if foo() just refuses be turned into a well-behaved Python

    while 1:
        x = foo()
        if not x:

(this is the standard "loop-and-a-half" pydiom, and every python pro-
grammer should be able to identify it as such in a fraction of a second).

or for the perhaps-overly-clever hackers,

    for x in iter(lambda: foo() or None, None):

it's not like the lack of assignment-as-expression is forcing anyone to
duplicate code in today's Python.

also, in my experience, most assignment-as-expression mistakes are done
in if-statements, not while-statements (if not else because if statements are
a lot more common in most code).  the "but it cuts down on duplication"
argument doesn't really apply to if-statements.


More information about the Python-list mailing list