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():
        process(x)

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

    while 1:
        x = foo()
        if not x:
            break
        process(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):
        process(x)

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.

</F> 






More information about the Python-list mailing list