Re[2]: [Tutor] Comparative code questions: Python vs. Rebol

Magnus Lycka magnus@thinkware.se
Thu Jan 16 10:53:01 2003


At 16:46 2003-01-16 +0300, antonmuhin =ED=E0 rambler.ru wrote:
>As usual Magnus suggested nice solution :) I only think that Magnus's
>solution is better situated for more complex problems.

Yes of course, it's one line longer than yours... ;)

It's largely a matter of personal preference of course,
but you might see that it *will* grow more complex soon if
you do something like this. After a while you will need
to set attributes in your tags, and do things like:

H1('The Beginning', CLASS =3D 'important', onclick =3D
    "javascript:openWin('beginning.html')")

You'll probably want to be able to set and change default
attributes as well, as in:

H1 =3D Tags('H1', CLASS=3D'important')
page =3D []
page.append(H1('Chapter 1'))
page.append(H1('Chapter 2'))
H1.CLASS =3D 'no big deal'
page.append(H1('Appendix'))
print '\n'.join(page)
<H1 CLASS=3D"important">Chapter 1</H1>
<H1 CLASS=3D"important">Chapter 2</H1>
<H1 CLASS=3D"no big deal">Appendix</H1>

Which path is easier to get here? Nested functions or classes?

It's always tricky to determine what features to use, at
least when other people who are not so familiar with
python will be involved in the code. I don't have any
qualms about using classes though, and certainly not
about using __init__... I think it's good to expose __call__
to people as well. It's not very difficult to understand,
is it? The sooner they get used to using classes, the
better! I might well completely leave out lambdas, map,
filter and reduce though. I have come to like them, but
they are really redundant sugar. They introduce new concepts
that needs to be learned without providing any really new
features. Classes, on the other hand, really add something
to Python.

 >>> class Tag:
...     def __init__(self, tag, **kwargs):
...             self._tag =3D tag
...             for attr, val in kwargs.items():
...                     setattr(self, attr, val)
...     def __call__(self, text):
...             return "<%s %s>%s</%s>" % (self._tag,
...                    self.attributes(), text, self._tag)
...     def attributes(self):
...             return " ".join(['%s=3D"%s"' % pair
...                             for pair in self.__dict__.items()
...                             if pair[0][0]!=3D'_'])
...
 >>> H1 =3D Tag('H1', CLASS =3D "important")
 >>> H1('A Beginning is a very delicate time...')
'<H1 CLASS=3D"important">A Beginning is a very delicate time...</H1>'
 >>> del H1.CLASS
 >>> H1('Know then, that I am princess Irulan, daughter of the Padishah=20
emperor Shaddam IV')
'<H1 >Know then, that I am princess Irulan, daughter of the Padishah=20
emperor Shaddam IV</H1>'

Implementing attributes in __call__ as well is left as an
exercise to the reader. Soon, this class will grow fairly big,
and you'll wonder why you didn't pick Andy Dustman's HyperText
from the beginning... ;) Then you wouldn't have to maintain all
that code...


--=20
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se