It's ...

Angus Rodgers twirlip at
Wed Jun 24 23:43:01 CEST 2009

On Wed, 24 Jun 2009 14:10:54 -0700, Scott David Daniels
<Scott.Daniels at Acm.Org> wrote:

>Angus Rodgers wrote:
>> from types import StringType   # Is this awkwardness necessary?

I'm starting to see some of the mental haze that was confusing me.

>Also, expandtabs is an instance method, so the roundabout is not needed.
>     def detab(s):
>         return s.expandtabs(stop)

I'd forgotten where Beazley had explained that "methods such as
... s.expandtabs() always return a new string as opposed to mod-
ifying the string s."  I must have been hazily thinking of it as
somehow modifying s, even though my awkward code itself depended
on a vague understanding that it didn't.  No point in nailing
this polly to the perch any more!

>I'd simply use:
>     for line in f:
>         print detab(line.rstrip())
>or even:
>     for line in f:
>         print line.rstrip().expandtabs(stop)

I'll read up on iterating through files, somewhere online for
the moment, and then get a more up-to-date textbook.

And I'll try not too ask too many silly questions like this, but
I wanted to make sure I wasn't getting into any bad programming
habits right at the start - and it's a good thing I did, because
I was!

>Nope.  But you could use a generator expression if you wanted:
>      g.writelines(detab(line) for line in f)

Ah, so that actually does what I was fondly hoping my code would
do.  Thanks!  I must learn about these "generator" thingies.

Angus Rodgers

More information about the Python-list mailing list