[Tutor] Writing one-liners in Python

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri, 03 May 2002 10:04:22 -0700 (PDT)


On 03-May-2002 Jeff Shannon wrote:
> 
> 
> Sean 'Shaleh' Perry wrote:
> 
>> > [Alan Gauld]
>> > Many things I'd do in Perl as a 'one liner' I do in Python
>> > by starting up the interactive prompt and typing the script
>> > one line at a time. It's a throwaway program like a one liner
>> > but more powerful too.
>>
>> This requires too much mucking about with files and import statements.  If I
>> have to go to this level I usually just write a script.
> 
> I don't see how the interactive prompt requires any more mucking about than
> writing a script does.  For simple tasks, especially if I'm not sure what I'm
> doing to start with, I find the interactive prompt *very* useful, since I can
> create objects and then fiddle with them to see what exactly they do.  And
> one
> of my normal methods of *writing* a script, is to try each bit out in
> interactive mode first, as I go, to see what works, and then copying that to
> a
> script window.
> 

usually I can type awk '{print $1" "$5"}' faster than the equivalent python. 
Handling many simple things requires an import statement.  So if I am going to
use python and import what I need I tend to do this in a file where I can reuse
the results.

I too perform rapid testing and experimentation with the interactive
interpreter.  It is one of the reasons I like python.  But there is often a
better tool for the job when I just need simple file handling done which is the
usual use of one liners.