[Tutor] Comparative code questions: Python vs. Rebol
Scott Widney
SWidney@ci.las-vegas.nv.us
Wed Jan 15 15:20:11 2003
> 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>'
> >>>
Or alternatively (and also pythonically):
>>> def H(level, text):
... return "<H%(level)d>%(text)s</H%(level)d>" % vars()
...
>>> print H(1, "This is level 1")
<H1>This is level 1</H1>
>>> print H(3, "This is level 3")
<H3>This is level 3</H3>
But now here's a challenge: is it possible, in the hypothetical context of
these examples, to write the function so that you could enter:
>>> print H(3, "This is level ?_? ")
^ # not sure what would go here
and receive:
<H3>This is level 3</H3>
Wait! Give me a few minutes....
>>> def HH(level,text):
... temp = "<H%(level)d>%(text)s</H%(level)d>" % vars()
... temp = eval('"'+temp+'" % vars()')
... return temp
...
>>> print HH(3, "This is level %(level)s")
<H3>This is level 3</H3>
>>> print HH(6, "This is level 6")
<H6>This is level 6</H6>
>>>
Whoa. That's dangerous.
...head aches now; must find analgesics....
Scott