Python syntax here : 2.7 - online REPL
Javascript ES6 via Babel transpilation - online REPL
import math
print math.log(42)
from math import log
print log(42)
# not a good practice (pollutes local scope) :
from math import *
print log(42)
import math from 'math';
console.log(math.log(42));
import { log } from 'math';
console.log(log(42));
import * from 'math';
console.log(log(42));
print range(5)
# 0, 1, 2, 3, 4
console.log(Array.from(new Array(5), (x,i) => i));
// 0, 1, 2, 3, 4
def foo():
yield 1
yield 2
yield 3
function *foo() {
yield 1;
yield 2;
yield 3;
}
lambda a: a * 2
a => a * 2
status, data = getResult()
var [status, data] = getResult();
search_db(**parameters)
searchDb(...parameters);
def fibonacci():
pre, cur = 0, 1
while True:
pre, cur = cur, pre + cur
yield cur
for x in fibonacci():
if (x > 1000):
break
print x,
var fibonacci = {
[Symbol.iterator]: function*() {
var pre = 0, cur = 1;
for (;;) {
var temp = pre;
pre = cur;
cur += temp;
yield cur;
}
}
}
for (var n of fibonacci) {
if (n > 1000)
break;
console.log(n);
}
(Python has builtin support for multiple inheritance)
class SpiderMan(Human, SuperHero):
def __init__(self, age):
super(SpiderMan, self).__init__(age)
self.age = age
def attack(self):
print 'launch web'
class SpiderMan extends SuperHero {
constructor(age) {
super();
this.age = age;
}
attack() {
console.log('launch web')
}
}
names = [c.name for c in customers if c.admin]
(Experimental in Babel)
var names = [for (c of customers) if (c.admin) c.name];
map(lambda: x*2, [1,2,3,4])
[1,2,3,4].map(x => x*2)
len([])
[].length
help(anything)
: get docstring for any module/method/function- list comprehensions, class magic methods !
- very powerful OOP
- huge and coherent standard library, ex : string has 38 useful methods
- built-in strings and array slicing.
- Builtin JSON support
- NPM packaging is a killer-feature : simple and fast, light-years ahead pip+virtualenv.
- Works in the browser :)
Just randomly found this gist, I like js but the comments are not super factually correct IMHO
python uses nonlocal statement for safety
but nobody would use that in a language that supports multiple inheritance, also js don't have a native decoration syntax sugar
not sure what is the feature, if it's yield syntax then python has it since 2001, but now I'm gladly using it in js ~15 years later.
len in python is not a list property it's a protocol
__len__
that is supported by practically everything that has length (dicts, orm queries, scientific data-structures).also don't forget the JS specific quirks
python map also can be used for any iterable object which is most of python data structures, sets, dicts, tuples, not just list, like fibonacci in the last example
map(lambda x: x*2, fibonacci(1000))
yeah like since 2006
I'm pretty much sure that js generator features were modeled after python because of how much they're similar
That being said js is a nice language (but with some abysmal old flaws like all numbers are floats by default or you define a variable in a global scope by default, or you need to actually learn how to correctly iterate over various things).