Python for RAD (was: [Tutor] Re: q about method(self))

Patrick K. O'Brien pobrien@orbtech.com
Sun, 1 Sep 2002 10:01:20 -0500


[Erik Price]
>
> I've never done anything like write a program in a scripting language
> to test a design model which is to be later implemented in another
> language.  I've never even seen code like this, either.  So here is my
> question:  If you are eventually planning to migrate code to, say Java
> (since that is the only other OO language I am familiar with), is it
> unwise to take the easier route of writing functions to do some work
> that would normally be implemented [more tediously] using a class?
>
> I would think so, since this would violate the very thing which is
> being tested -- the design/model of the application.  Obviously the
> design should have accounted for the need for this feature and there
> should be a class to represent it (or it should be handled somehow
> without the need for a separate function, since this won't be possible
> in the final implementation language [Java]).  At least, that is what I
> would assume.
>
> But if you're staying within the Python language then a pure
> object-oriented methodology of using only classes might be more trouble
> than it's worth, it would seem.

Fortunately, Python makes it easy to take either of these paths. Defining
all actions as methods instead of functions only requires a trivial amount
of extra code. So the overhead in Python is minimal. Here is a (necessarily
oversimplified) example of something coded as both a function and then as a
method of a class:

>>> def p(text):
...     return '<p>%s</p>' % text
...
>>> p("This is a paragraph.")
'<p>This is a paragraph.</p>'
>>> class Html:
...     def p(self, text):
...         return '<p>%s</p>' % text
...
>>> html = Html()
>>> html.p("This is another paragraph.")
'<p>This is another paragraph.</p>'
>>>

>From a pure design point of view, I actually think Python's use of functions
and modules is a more practical reflection of reality than forcing
everything to be a method of a class. It also does a better job of
supporting code reuse. One way to look at this is to see a function as a
kind of method that has no need for any state associated with its own class
instance, and therefore should be able to stand on its own. A function
doesn't have a compelling *need* to *be* a method. If it did, then it should
be coded as a method. Forcing it to be a method of a class is easy enough to
do in Python, it just doesn't buy you anything if there is no need.

But if you know that this is a prototype for another language, then I would
go ahead and code it as a method in Python as well. That way you'll get the
experience of actually using the code as a method call, rather than a
function call, and you'll get a better sense of whether the organization of
these function/method hybrids is elegant and useful.

Hope that helps.

--
Patrick K. O'Brien
Orbtech
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------
Web:  http://www.orbtech.com/web/pobrien/
Blog: http://www.orbtech.com/blog/pobrien/
Wiki: http://www.orbtech.com/wiki/PatrickOBrien
-----------------------------------------------