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

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Thu Oct 18 23:28:59 EDT 2001


On Thu, 18 Oct 2001 17:15:04 -0700, Jeff Shannon <jeff at ccvcorp.com> wrote:
>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.....

Thank you (and several others) for pointing this out.  I misunderstood the
original code.  It is interesting that your interpretation is also wrong -
condition line[3:4]=='-' was not inverted - what was inverted is code==[:3].
Is this an indication of the (lack of) readability for using "if not: break"
in place of "while"?  :-)

The original code is quite enigmatic to me.  I don't know the ftp protocol,
but from Andrew's example data I think the code is intended to be

    def getmultiline(self):
        code = None
        lines = []
        while line = self.getline(); \
            lines.append(line); \
            line[3:4] == '-' or code != line[:3]:
                code = line[:3]
        return '\n'.join(lines)

I'm not sure if this is correc, either, except it works on Andrew's data.
(What should happen if code changes when line[3:4]=='-', etc?)

realizing-spent-too-long-on-this-topic-and-running-away-ly yr's

Huaiyu



More information about the Python-list mailing list