Brython - Python in the browser

Chris Angelico rosuav at gmail.com
Fri Dec 21 13:57:58 CET 2012


On Fri, Dec 21, 2012 at 11:38 PM, Pierre Quentel
<pierre.quentel at gmail.com> wrote:
> With the tree syntax proposed in Brython it would just be
>
> doc <= DIV('hello '+B('world'))
>
> If "pythonic" means concise and readable, which one is more pythonic ?

Pythonic also means:
If the implementation is hard to explain, it's a bad idea.

What, exactly, does the sum of a string and a bolded string produce?
Can you explain that easily and clearly? Don't forget that humans, as
well as machines, will expect to be able to parse what's inside the
parentheses without looking outside them.

The DOM structure is, undeniably, quite verbose. But you could go for
something with the same tree structure while a lot less wordy by
simply returning self from lots of methods, thus allowing method
chaining - something like this:

https://github.com/Rosuav/Gypsum/blob/master/window.pike#L247

Note how the tree structure is defined by the parentheses, with
->add(...) calls creating children. (That's Pike, not Python, as I
don't have a good Python example handy. The -> operator does more or
less what Python's . does.) Now, I can conceive of a kind of API - an
ideal API, the creature of my fancy, you know - which would be totally
unobjectionable. An API, for instance, which would abolish taxes and
make everything cheap, except gondolas... oh wait, wrong mailing list.

To produce the HTML code

<DIV>hello <B>world</B></DIV>

you might use:

doc.add(Tag('DIV').add('hello ').add(Tag('B').add('world')))

And you can easily wrap that to suit, since it's surrounded by parentheses:

doc.add(
  Tag('DIV')
    .add('hello ')
    .add(Tag('B').add('world'))
)

There's no magic with operators, just simple method chaining. It's a
bit more verbose than your proposed tree syntax, but not nearly as bad
as the JS version; and it's not magical.

Reject the idea if you will, but do at least please consider it :)

ChrisA



More information about the Python-list mailing list