[Tutor] Comparative code questions: Python vs. Rebol

Tim Johnson tim@johnsons-web.com
Wed Jan 15 15:44:08 2003


* Jeff Shannon <jeff@ccvcorp.com> [030115 10:40]:
> 
> 
> Tim Johnson wrote:
> 
> >Now here is a more complex example:
> >Dynamically compose a series of functions such that
> ># python 
> > print H3("This an html header of level 3")
> > >>> "<H3>This is an html header of level 3</H3>"
> >
> 
> Others have shown how you can make Python do what you ask, here. 
> Instead of repeating that, I'll show you a way to achieve much the same 
> ends, but in a more Pythonic way -- something that I think makes it 
> easier to read, understand, and thus mantain.
> 
> Instead of having a series of very similar functions that make 
> almost-equivalent modifications to a string, it's much simpler to have a 
> *single* function that takes a parameter to indicate which of several 
> modifications to make.
> 
> >>> def HtmlHeader(level, text):
> ...     return "<H%d>%s</H%d>" % (level, text, level)
> ...
> >>> HtmlHeader(1, "This is level 1")
> '<H1>This is level 1</H1>'
> >>> HtmlHeader(3, "This is level 3")
> '<H3>This is level 3</H3>'
> >>> HtmlHeader(6, "This is level 6")
> '<H6>This is level 6</H6>'
> >>>
> 
> The % operator, in this context, will make substitutions in the string 
> in a manner similar to C's printf() formatting.  By doing it this way, 
> we have only one function to keep track of, instead of six.  This is 
> easier on both the writer of the library (this is simpler than the 
> dynamic composition), and on the user of the library who has five less 
> functions to keep track of.  (In production code, I'd insert some sanity 
> checking to ensure, for example, that the level was an integer value 
> between one and six [inclusive], but that seems overkill for example code.)
> 
> Jeff Shannon
> Technician/Programmer
> Credit International

  Hi Jeff:
    Thank you for the additional info. In truth, I agree and
    that is the approach that I would use, however my 
    original query was to generate some material for a
    column that I'm writting.

    Thanks!
    -tim-
-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com
      http://www.johnsons-web.com