What is Pythonic?

Occasionally I get told that RapydScript is not Pythonic enough, because I don’t support a certain feature of Python. For example, because I don’t handle string interpolation out of the box. I don’t do this because there are already JavaScript alternatives that do a very good job (http://www.diveintojavascript.com/projects/javascript-sprintf), and I don’t like reinventing the wheel. The purpose of RapydScript is not to mimic Python functionality exactly, but to make JavaScript development more sane for those who’re used to Python (or can’t stand CoffeeScript’s Perl-like syntax).

RapydScript takes a different approach from other Python-in-a-browser frameworks, I do not try to reproduce Python functionality in a browser, and in fact Guido himself argued against that for the same reason I argued against Pyjamas, see his answer to “Python in the browser?” from this interview: http://developers.slashdot.org/story/13/08/25/2115204/interviews-guido-van-rossum-answers-your-questions

The %-based syntax simply doesn’t make sense to implement, because % operator is already reserved for modulo division both in JavaScript and in Python. Overriding that would require a new function, something along the lines of _$rapyd$_interpolation_or_modulo(), which would need to check at runtime what the user meant to do based on arguments. And this kind of overhead for the logic that’s already achievable via a more explicit sprintf is why Pyjamas apps were a pain to debug. The "{}".format("foo") syntax, on the other hand, I have no problem with, it requires no compiler changes and does not conflict with existing functionality. I don’t need that syntax because sprintf plugin does the job for me, but feel free to submit a library that implements it yourself if you need it. That is after all, how open-source works.

At the end of the day, you should ask yourself: What is “pythonic”? Is it something that reproduces Python’s functionality exactly? No, because Python itself evolves. Is Python 2 more pythonic than Python 3 because it was there first or is Python 3 more pythonic than Python 2 because it simplifies parts that might not have made sense? Is something pythonic because it follows The Zen of Python? If so, then isn’t an explicit sprintf more pythonic than % operator, which can be confused with modulo division based on context? I certainly think so.

Since its inception, Python’s main selling point was how similar it was to pseudocode that you would see in textbooks, not its shortcuts. RapydScript tries to stay true to that philosophy, by cleaning up parts of JavaScript that don’t make sense, and leaving the parts that do alone. That’s the main difference between RapydScript and regular Python-in-a-browser compilers. RapydScript doesn’t try to replicate all of Python syntax, that’s a fool’s errand. RapydScript tries to introduce the clarity of Python into JavaScript.

At the end of the day, you have to realize that JavaScript did not start developing its main feature-set until a few years ago. As a result, it could learn from mistakes of many other languages, including Python. If “pythonic” is something that follows Python’s philosophy and simplicity, then in some ways, the new JavaScript is actually more pythonic than Python. If you don’t believe me, look at CasperJS and compare its simplicity against any of Python’s web-scraping alternatives. Better yet, compare the simplicity of building a GUI with HTML5 and jQuery vs building a GUI in Python.

This entry was posted in Languages and tagged , by Alexander Tsepkov. Bookmark the permalink.

About Alexander Tsepkov

Founder and CEO of Pyjeon. He started out with C++, but switched to Python as his main programming language due to its clean syntax and productivity. He often uses other languages for his work as well, such as JavaScript, Perl, and RapydScript. His posts tend to cover user experience, design considerations, languages, web development, Linux environment, as well as challenges of running a start-up.

8 thoughts on “What is Pythonic?

  1. Hy Alex,

    Happy new year :-) I can’t agree anymore. The more I use RapydScript the more I like it. The main feature I like is how well it integrates with third party Javascript libraries.

  2. I agree that "{}".format("foo") is the real Pythonic;) FWIW, PythonJS provides it out of the box. How would you compare RapydScript to PythonJS? As far as I understand, the approach is similar (and very different from Brython) ..

    After reading your blog post, it’s somehow apperant to me, that there are different kinds of goals or expectations that one might have – at least:

    • bring a good chunk of the Python language fun and sanity to the browser .. without replicating Python in the browser necessarily
    • achieve source code compatibility for a restricted set of sources (under certain restrictions like number semantics etc which would be too expensive to replicate unless you go the Brython way; and not replicating most Python batteries other than the most fundamental .. like eg collections)

    Both are different. Both are interesting to me.

    Btw: thanks for this open-source effort!

    • Exactly, and having played with Pyjamas and other frameworks, I realized that it was really the first bullet that I missed from Python when dealing with JavaScript. My main problem with JavaScript was that its source code structure wasn’t originally designed to scale for large projects. The missing syntax sugar shortcuts can be annoying, I’ll admit, but RapydScript’s stdlib tries to partially remedy that (via string/dict/list and things like deep_equal() method).

      Complete source compatibility, while a fun project, has proven itself time and time again to not be feasible (Pyjamas, Brython). You would really need to wrap every JavaScript object with your own, that behaves more like Python. This id definitely doable, and that’s exactly what Pyjamas has done, but there are two problems with this approach: 1: you now lose compatibility with JavaScript native libraries, which there are a lot more of than your own 2: your code starts running several times slower, especially on mobile devices

      In the end, the language that adapts to the ecosystem it exists in best wins. And the ecosystem in the browser is JavaScript and DOM.

  3. Hi ! RapydScript looks very promising. What I would love to see is an online converter JS PYJ and (easy enough !) a tag on StackOverflow. Those are two things that would help reassure my colleagues (and me too !) and would help to do the switch. Congrats anyway

    ps: I didn’t find the demo page very convenient too (was expecting code and demo side by side)

    • There is a decompiler Charles put together (https://github.com/charleslaw/RapydScript-Decompiler), and I’ve applied multiple fixes to it to make sure the generated RapydScript works in most cases. It still breaks in some cases (scope assumed to always be local, it won’t handle funky ways of writing for loops gracefully, and i++ is actually handled as ++i). Still, it works well for the most part, I’m still in the process of porting the compiler using it into RapydScript. Really, however, unless you want to clean up existing code, you don’t need to port your old JavaScript to RapydScript for it to be compatible. As far as online demo for seeing the page deployed, what I’ve been doing in those cases is compile the code using our regular online demo/compiler on the website and then copy-paste it into jsfiddle.net. I’d also like to get a tag on StackOverflow, but I haven’t looked into that yet. Do you know how the process works?

  4. I think it is good to have minimal support for at least: "foo%sbar" %X and "foo%sbar%s" %(X,Y)

    its probably mostly just me, but i use that syntax most of the time, even though i agree it is not the best pythonic style.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>