statements in control structures (Re: Conditional Expressions don't solve the problem)

Jeff Shannon jeff at ccvcorp.com
Thu Oct 18 20:15:04 EDT 2001


Huaiyu Zhu wrote:

>  So you would have to write
>
> A
> loop:
>    B
>    if not C1:
>       E1
>       break
>    D1
>    if C2: break
>    D2
> E2
>
> Or you have to enclose the whole thing in another function and use "return"
> as the name of another "break".

Or you could decide that if you're doing something this complex, it would be
much more readable if split up into smaller functions, or otherwise reorganized
for clarity.

As for your examples:

Lib/ftplib.py:
--------------------------------------------(3)
    def getmultiline(self):
        line = self.getline()
        if line[3:4] == '-':
            code = line[:3]
            while 1:
                nextline = self.getline()
                line = line + ('\n' + nextline)
                if nextline[:3] == code and \
                        nextline[3:4] != '-':
                    break
        return line

changed to

    def getmultiline(self):
        code = None
        lines = []
        while line = self.getline(); lines.append(line); line[3:4] == '-':
#?????
            if code != line[:3]: break
            else: code = line[:3]
        return '\n'.join(lines)
----------------
Just looking at that line makes my brain want to explode.  Yeah, I could
probably struggle my way through understanding what's going on, but...  ick.

I will admit that the original code could be rewritten to be clearer, but I do
*not* find your version to be at all clearer--quite the opposite.

.... hm, in sorting through this, it looks to me like you've got it wrong,
anyhow.  The first line should be checked if line[3:4] *is* a '-', and if so,
the following line(s) are added, but in the while loop, further lines are added
only if there is *not* a '-' at [3:4] .... the loop condition is inverted after
the first line.  And your version will *never* have more than one line, because
the first time through the body of your loop, code (None) will never be equal to
line[:3], so you will *always* break.

So, not only is it ugly and difficult to understand, it's wrong.  Which would
have been far more apparent if it were *not* so difficult to understand.....

Jeff Shannon
Technician/Programmer
Credit International







More information about the Python-list mailing list