Brython - Python in the browser

Pierre Quentel pierre.quentel at
Fri Dec 21 21:59:19 CET 2012

> The interpreter, though, will be more than happy to treat that as a
> comparison if the LHS is not the type that you think it is.  For
> example, maybe you've added it to a string at some point, and now it's
> a string instead of an element.  I guess that since doc is made a
> keyword, that probably couldn't happen in this example, but it could
> happen when trying to add child nodes to other nodes.

Unsurprisingly, the translation engine in Brython transforms x <= y into x.__le__(y)

If x is a string, then __le__ means of course "lesser or equal" so y can only be a string, otherwise an exception is raised ; this is similar to trying to add a child node to a text node in the DOM

> By the way, what is Brython actually doing when you append a child to
> the document itself like that?  Usually I would expect a div to be
> appended to the body or to another div.  The above looks like it would
> attach the new div as a sibling of the html element.  Or is it just
> calling document.write()?

dom_elt <= obj actually adds one or several DOM nodes (it depends of the class of obj) to the DOM node represented by dom_elt. It's difficult to explain all the cases here, you would have to take a look at the code in py_dom.js, but <= and + work on the DOM tree, there is no document.write anywhere

More information about the Python-list mailing list