[Tutor] More OOP

Karl Fast karl.fast at pobox.com
Fri Nov 7 10:41:15 EST 2003



> I question if I'm doing the right thing not to dive into GUI making
> to much this early in my learning ladder. 

This is the story about how GUI programming helped me learn the
basics of OOP (I posted a real short version yesterday).

For my project, where I learned python and everything, I was asked
to create a simple GUI. No CLI, I was told. And I need to be able to
pause, continue, and cancel the spider.

I did *not* start with the GUI. I starting by writing the code as a
basic commnand line spider (like I'd done in Perl in a previous
project). It was rudimentary, but it worked. I just needed to get a
feel for the language. My code was wholly procedural. I avoid
classes.

My thinking was "hmmm, it's probably a bad idea to learn python,
oop, wxPython + gui programming + event driven programming, and COM
all in one go." (COM was for reading/writing directly to Excel)

So my initial focus was python and COM-to-excel for data storage. 
Since I had written a spider in perl this was largely a matter of
translation. This was a good exercise, not too difficult, and
exposed me to a lot of standard modules in python. The COM bit was
completely new to me. It was also confusing. I got the COM piece
working thanks to (a) a small example in Learning Python and (b)
examples I dug up googling. 

Now I had a basic spider in python that would read/write data to
excel. It didn't have all the features I needed, but it worked, I
understood it, and if I saw some python I could understand most of
it. I felt sufficiently confident in my python skills to move on.

So I moved on to creating a basic GUI. Again, I kept it real simple.
I used wxGlade to create my GUI code (I considered Boa, PythonCard,
and wxDesigner too). Then I started wiring it together.

I followed examples in the wxPython wiki. I read the sections on OOP
and classes in my books (Learning Python and Core Python
Programming). I began experimenting. It was frustrating at times,
but I kept plugging away. Slowly, the learning curve flattened.

The GUI stuff was OO-heavy. No way to avoid it. And when I wired my
other code in I got something that worked, but it didn't feel right.
It felt ugly and as I kept adding things, it was getting uglier. It
worked, but gawd...what a mess.

I began rewriting my procedural code as classes. The GUI stuff
wasn't forcing me down this road, but it sure made this road more
appealing. My previous code, the procedural stuff, worked fine. It
instantiated objects from other modules, of course, but I had never
found a compelling need to step outside my procedural comfort zone
and write my own classes.

Building a wxPython-based GUI changed all that. Suddenly it was
worthing switching my mindset from a procedural worldview to an OOP
one. Suddently OOP was the path of least resistance. A complete
switch. Yes, OOP would have made previous projects easier, but when
I added in the cost of learning how to do this new math, the answer
was always the same: you gotta get this done, you know how to do it
procedurally, so just get it done and move on. Yet here was a
situation where when the equation was giving me a different answer.

For me, learning wxPython (Tkinter would have been the same) has
been an excellent way to learn how to write my own classes. Of
course there are piles of things I don't know and many aspects of
OOP that elude me. But I can see the value of it now. I don't
hesitate to create a new class.

The other day I began working with threads and right off I
subclassed the threading module. It was easy and felt natural. A few
weeks ago I would have been comfortable instantiating a threading
object, but that's about all. The idea of subclassing would have
given me pause. No more.

I feel like one of those weight loss ads: "I learned OOP in 21 days,
and you can too. Just switch to Python, build a little GUI, eat a
little sushi, and voila!"



--karl




More information about the Tutor mailing list