Brython - Python in the browser

Ian Kelly ian.g.kelly at
Sat Dec 22 00:45:03 CET 2012

On Fri, Dec 21, 2012 at 3:52 PM, Chris Angelico <rosuav at> wrote:
> On Sat, Dec 22, 2012 at 3:36 AM, Pierre Quentel
> <pierre.quentel at> wrote:
>>> Hmm. So when that gets added into a DIV, it has to get parsed for
>>> tags? How does this work? This seems very odd. I would have expected
>>> it to remain as DOM objects.
>> In DIV(child) :
>> - if child is a string, integer or float, a text node is added (addChild) to the DIV element, with the string value of child
>> - if child is another DOM element (as in DIV(B('foo'))) then this element is added to the DIV element
> Meaning that:
> doc <= <p></p>'
> will add literal text, not a paragraph object, right? That's
> definitely what I would expect.
>> doc <= 'blah blah x<y: '+B('True!')
>> It will add a text node to the document, with the string 'blah blah x<y: ' followed by 'True!' in bold characters
> This is where it's getting confusing. My expectation of this is that
> it adds a text node with the literal text, followed by a bold node
> with its child text. This operation should never involve the parsing
> of HTML tags, as the document structure is all there in the code. So
> it ought to be a DOM object, not a text string, that gets <='d onto
> doc (is <= a verb now?). That means the result of the addition has to
> be a DOM object, not a text string; but you said that adding a string
> to a B object converts the object to a string and concatenates the
> strings.
> Do you see now what I mean about the API being difficult to explain?

In my playing around with it just now, the addition doesn't seem to
actually return a string.  I typed this script into the test console:

log('hello ' + B('world'))

and clicked Run, and the result was:

[object Object]

whereas if I just try to log a plain string literal, it actually
prints out the string.  Somewhat disturbingly, this also gives the
[object Object] result:

log(str('hello ' + B('world')))

In Brython, the str builtin does not return strings?

More information about the Python-list mailing list