JavaScript is almost pythonic

Masataka Arai on December 05, 2017

Multi-line String Python3.6 print("""string text line 1 string text line 2""") ES2017 console.log(`string text line 1 st... [Read Full]
 

I just really want indentation instead of brackets for code blocks! Then JavaScript will officially be fully Pythonic.

 
 

True, CoffeeScript is heavily influenced by Python! I do think it usually causes more pain than its worth to add something like that in though.

According to wikipedia: "CoffeeScript is a programming language that transcompiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python and Haskell in an effort to enhance JavaScript's brevity and readability."

The indentation is definitely Pythonic.

 

Yep, CoffeeScript is a good idea except that this kind of syntax-driven transpiling never seems to be worth it. Maybe pre-ES2015 but I can't imagine CoffeeScript living much longer besides legacy.

I disagree. If you have to use a build step to convert JavaScript into older JavaScript to get all of it's best features using another syntax is just as okay. Use Elm use ClojureScript use CoffeeScript use whatever you're comfortable with.

I agree with you, but what I was trying to say is that with JS's improvements, CoffeeScript isn't offering as much. A lot of the benefits are now native, so the CoffeeScript community shrinks and the tradeoffs of fewer people understanding it are no longer worth the trouble.

Elm and ClojureScript bring more than syntactic sugar to the table. Same with TypeScript.

Use CoffeeScript if that's what you like, but I think the benefits have faded of late.

I haven't really used it myself but the community seems to have some really dedicated people. I believe they just released a new version to put it more in line with modern js.

So I'd hate to stop a potential user from trying it because they read this and didn't think it was worth a glance.

The JS comminity owes them a lot cause es6 wouldn't be what it is without coffeescript so hopefully they keep doing innovative stuff that ends up coming over to us.

The JS comminity owes them a lot cause es6 wouldn't be what it is without coffeescript so hopefully they keep doing innovative stuff that ends up coming over to us.

I think this is a little off the mark. This thread was discussed the influencing factors for coffeescript and this post showed how js is similar to a coffeescript influence (python). Personally, I think it's more accurate to thank python for doing innovative stuff that "ends up coming over to us". Repositories like this highlight how cs is waning.

 

How about BuckleScript, a OCaml compiler back-end that eats OCaml and spits out JavaScript?

Looks pretty Pythonic to me:

let rec hanoi n a b c =
  if n > 0
  then
    (hanoi (n - 1) a c b;
     Js.log {j|Move disk from pole $a to pole $b|j};
     hanoi (n - 1) c b a)
let _ = hanoi 4 1 2 3
 

They actually share roots! OCaml and Elm are both in the ML family of programming languages.

I had a better time learning Reason and BuckleScript than learning Elm though LOL but hey as long as it's functional I'm all for it

 

Yeah plus all the types and all the goodness 😄

 
 

True, this is kind of half joke, half dream scenario. You would probably need automatic bracket insertion similar to the automatic semi-colon insertion that exists now!

 
 

Do you know if JavaScript has anything approaching comprehension syntax? That would be extra pythonic!

From your "Arrow Function" section:

numbers = [1, 2, 3, 4]
list(map(lambda x: x * 2, numbers))

Generally (and you probably know this), you see this in Python as:

numbers = [1, 2, 3, 4]
[x * 2 for x in numbers]
# => [2, 4, 6, 8]
 

Array comprehensions existed briefly but didn't receive enough support to become standardized so they were dropped. I expect they may come back in the future.

 

Yeah, I don't think the syntax was quite as clean as Python's but I love that feature in Python and hope it comes back to JavaScript.

 

You can get rid of the semicolons as well. I use prettier in VS Code and it automatically does it for you.

 

Newbie question, what is the performance hit for auto insertion?
I mean when the interpreter does it, "live"

lh3.googleusercontent.com/hX39h-wD...
If it's a large app, this seems like a lot 😅

 
 

It kinda seems like languages are all becoming each other, haha. I can draw a lot of similarities here to Dart. I'm sure some folks would find that a lot of the same features listed here are also in other languages like Kotlin and Swift.

 
 

To add to the Getter & Setter examples: You can use this in Python instead of self. It is just a convention, that people refer to the current instance as self. Also, instead of sqrt in both languages, you can write **0.5. With both modifications, that codes look even more similar.

About the classes example: They might look similar, but they are not. __init__ is not a constructor. __init__ is an initializer. The constructor in Python is called __new__. Also, usually you override __repr__ to get a representation of the object instead of __str__.

 

Hmm... Good comparaison, JS is definitely getting better, but still.

Quick thougts:

Everything is an Object in JS (including arrays), there isn't any proper equivalent of python's magic functions (add for example).

One thing that horrifies me: how to convert a integer to string: '' + 2.

I think JS' ecosystem is awesome, but JS in itself absolutely sucks. Pretty much the opposite of Python.

 

There's a method for that:

const x = 2;
x.toString(); // "2"
 

Or as an alternative, String(x), which does exactly the same thing, except if x is null or undefined, then it produces 'null' or 'undefined' instead of throwing an error.

 

No need to be that categorical in your judgement, your '' + 2 code sample has always been outdated, as String(2) or (2).toString() have been allowed since the 1997 standard (source).

It’s possible to do awful code in a lot of languages, Python included, finding the worst in a tech is not a proof of it “sucking” ;)

 

Javascript cannot be Pythonic until it has Python's rules of dynamic typing, mutability/immutability, and equivalence. It would also need to iterate in the same manner. See Facts and myths about Python names and values and Loop Like a Native by Ned Batchelder for excellent explanations.

 

I'd better keep apart semantics and tinkering with lang:

JS:

0 in [1,2,3] === true
1 in [1,2,3] === true
// Wrong usage because JS expects Object

Python:

(0 in [1,2,3]) == False
(1 in [1,2,3]) == True

But, JS:

'1' in {'1': null} === true // correct

and Python:

(1 in {1: None}) == True

Thus historically JS is much more ad-hoc with all the consequences.

 

It takes more than a few specific capabilities to be "Pythonic".

PEP-20 for example is pretty core to Python: python.org/dev/peps/pep-0020/

I find it unlikely that JS's poor organization will ever reach a level where they could be considered Pythonic, even though they do make (often sloppy) copies of features from (among other things) Python.

 

:( Kinda gutted that JS extends syntax looked a lot cleaner.

I have to fight to not use braces if I switch between Python & JS, even though they waste space and the language doesn't enforce (eslint is a tool, it's not language enforced like python).

Here's my tweet response twitter.com/LewisCowles1/status/10...

 

Yeah, I've noticed this some time ago.
Don't forget that you also can omit semicolons in JS as well :)

 

I was worried that the variable declaration of the sample code is var though using ES2017.

 

Looks a lot more like C# to me. If ES2017 had access modifiers it'd be nearly identical.

 
 

I noticed these similarities when working with Django. So often I accidentally code with Python syntax in JS or vice versa

 

It sure makes JS more usable. Still, the "insides" keep on being messy, there is many holes around and so on. But yup it's moving towards a direction that I like.

 

Very interesting comparison, thanks for the nice clear examples.. :)

 

I'd say it's even more like Ruby, but everyone already says that Ruby and Python are similar.

 

Just waiting for ES202? == Python, you might as well...

 

Cool! Also, closure works the same in Python and Javascript.

 

The best thing that happened to me was when I presented some working python code and the guy on the front row asked me when I was going to implement the pseudocode example.

 
 
 

It looks more like C#. if you consider Typescript instead of JavaScript is uncanny similar

 
code of conduct - report abuse