How do you code in Python ???

Peter Hansen peter at engcorp.com
Fri May 17 19:34:28 EDT 2002


Shagshag wrote:
> 
> Newbie to python, i was wondering how *skilled* guys code with it :
> 
> By now to write a function i write it in a text editor (with coloured
> syntax), copy and paste it in idle try it, then go back to text
> editor to debug it and so on until i've tuned it like i want, then
> next function and so on until i complete module... And that's really
> boring theses permanent back and forth...
> 
> So how do you do it ? Which tools do you use ?

Basic text editors (Codewright at work) and heavy reliance as of
recently on the unittest module and Test-Driven Development.  See
the examples at http://www.diveintopython.org/roman_divein.html
for an excellent introduction.

Sequence is:

1. create unit test file
2. write first unit test, calling one method
3. run test: it should give an error (method not written yet!)
4. create file with method with simple 'pass' statement
5. run test: it should fail (not an error) because method is useless
6. code method until test passes
7. refactor method, checking test still passes
8. write next test for new functionality
9. run test .... 

Continue the above sequence until you have something that does
what is needed.  Now you're done, and you have a full suite of
unit tests, so you are fairly certain you can rely on the code
you've written.  

Also, with this approach, the code tends to be much more modular
and appears to have a better design than code developed the more
traditional way.

And you don't need a fancy set of tools for this, just a text
editor and your command prompt...

-Peter



More information about the Python-list mailing list