Why do I require an "elif" statement here?

danielx danielwong at berkeley.edu
Sun Aug 6 18:01:43 EDT 2006


No offense. I didn't mean there was anything wrong with your way, just
that it wasn't "neat". By that, I meant, you were still using lots of
for loops and if blocks.

Justin  Azoff wrote:
> danielx wrote:
> > I'm surprised no one has mentioned neat-er, more pythonic ways of doing
> > this. I'm also surprised no one mentioned regular expressions. Regular
> > expressions are really powerful for searching and manipulating text.

I suppose I put those things too close together. I meant I was
surprised for each of the two Separate (non)occurances.

> [snip]
>
> I'm surprised you don't count my post as a neat and pythonic way of
> doing this.  I'm also surprised that you mention regular expressions
> after neat and pythonic.  While regular expressions often serve a
> purpose, they are rarely neat.
>
> > Anyway, here's my solution, which does Not use regular expressions:
> >
> > def reindent(line):
> >     ## we use slicing, because we don't know how long line is
> >     head = line[:OLD_INDENT]
> >     tail = line[OLD_INDENT:]
> >     ## if line starts with Exactly so many spaces...
> >     if head == whitespace*OLD_INDENT and not tail.startswith(' '):
> >         return whitespace*NEW_INDENT + tail
> >     else: return line    # our default
> [snip]
>
> This function is broken.  Not only does it still rely on global
> variables to work, it does not actually reindent lines correctly.  Your

It's just an illustration.

> function only changes lines that start with exactly OLD_INDENT spaces,
> ignoring any lines that start with a multiple of OLD_INDENT.

Maybe I misread, but that's what I thought he wanted. Did you not see
my code comments expressing that very intent? Anyway, it wouldn't be
that hard to modify what I gave to do multiple replacement: just add a
recursive call.

> 
> -- 
> - Justin




More information about the Python-list mailing list