Friday, December 21, 2012

Python List comprehensions now in Brython

Brython improvements

So by now, you've heard of Brython, the implementation of Python 3 (or at least a subset) for your web browser, in Javascript. After all, I've been talking about it for almost a month now (although not in english).

New this week


Pierre has been busy... He implemented the ternary operator, that is, a way to assign one of two values, based on a condition, for example:

x = 7 if a > 2 else 17
List comprehensions are also in! This was a heavily requested feature that was missing from Brython, for example:

y = [ x ** 2 for x in range(10)]
 I did find a bug in some variations, and am looking into the code to fix this.

Another addition this week is that your python code indentation is now relative to the first line of your script, instead of having to start at the first position. That means you can now format your html code and have your Python properly aligned:

<!DOCTYPE html>
<html>
    <head>
        <title>Brython test</title>
        <script src="brython.js"></script>
    </head>
    <body onLoad="brython()">
        <script type="text/python">
        def myfunc():
            return True

        log(myfunc())
        </script>
    </body>
</html>

instead of:
        <script type="text/python">
def myfunc():
    return True

log(myfunc())
        </script>


4 comments:

James said...

Congrats! This is coming along nicely!

Perhaps it's time we get circuits + brython working in a Browser utilizing the full strength of components, loose coupling with a full messages passing system that ties into the DOM events.

Hmmm :)

--JamesMills / prologic

João Santos said...

When will you have an efficient implementation of dicts?

GAME ONLINE said...

That is really cool. Thanks for finding it. Some really good ideas there, this is going to be a real time sink. You can learn more: China tourism | China travel service | China vacation packages

Learn Chinese language on hanbridgemandarin said...

I'm learning mandarin Chinese because I believe it's the only way to really learn about China.When I was searching for a place to learn mandarin Chinese, I called several schools. Hanbridge was the best because they had excellent teachers and a very friendly and welcoming spirit. ?I really appreciate the opportunity to learn here and would recommend Hanbridge to others.