A comparison of declarative python and js

July 22, 2021

Declarative code communicates more effectively than imperative code 1. Declarative code is easier to read and reason about since it describes what the code does rather than how it does it.

Consider the following two pieces of code from Functional Light JS.

// declarative parameter specification
function foo([x, y, ...args] = []) {}

// imperative parameter specification
function foo(params) {
  var x = params[0];
  var y = params[1];
  var args = params.slice(2);
}

Both functions are doing the same thing but first function is easier to read. In the second function you need to read three lines of code to figure out what the function does, and you need to be familiar with how slice works.

Wherever possible, and to whatever degrees our language and our libraries/frameworks will let us, we should be striving for declarative, self-explanatory code. — Kyle Simpson

I recently started writing python code for the first time in a while, and I decided to compare how to write declarative syntax in python and js.

Creating a list

  • Python
    foo = [1, 2, 3]
    
  • JS
    var foo = [1, 2, 3];
    

Creating a Dictionary/Object

  • Python
    foo = {"a": 1, "b": 2, "c": 3}
    
  • JS
    var foo = { a: 1, b: 2, c: 3 };
    

Unpacking a list

  • Python
    a, b, c = [1, 2, 3]
    
  • JS
    var [a, b, c] = [1, 2, 3];
    

Both python and js support nested unpacking.

  • Python
    a, [b, c], d = [1, [2, 3], 4]
    
  • JS
    var [a, [b, c], d] = [1, [2, 3], 4];
    

and both python and js support merging multiple items together during unpacking.

  • Python
    a, *b = [1, 2, 3]
    # a = 1, b = [2, 3]
    
  • JS
    var [a, ...b] = [1, 2, 3];
    // a = 1, b = [2, 3]
    

One thing python does which javascript does not is allowing the merging parameter to be anywhere in the unpacking.

a, *b, c = [1, 2, 3, 4, 5]
# a = 1
# b = [2, 3, 4]
# c = 5

You can use this to get the last element in a list.

*_, last = [1, 2, 3, 4, 5]
# last = 5

Unpacking a Dictionary/Object

Python does not have support for unpacking a dictionary.

  • JS
    var { a, b, c } = { a: 1, b: 2, c: 3 };
    

Array Concatenation

  • Python
    foo = [*[1, 2, 3], *[4, 5, 6]]
    
  • JS
    var foo = [...[1, 2, 3], ...[4, 5, 6]];
    

Object/Dict Concatenation

  • Python
    foo = {**{"a": 1, "b": 2, "c": 3}, **{"d": 4, "e": 5, "f": 6}}
    
  • JS
    var foo = { ...{ a: 1, b: 2, c: 3 }, ...{ d: 4, e: 5, f: 6 } };
    

In both python and javascript later keys overwrite earlier keys when you merge dictionaries/objects.

Rest Parameters

  • Python

    def foo(*args):
      pass
    
  • JS

    function foo(...args) {}
    

Partial Rest Parameters

  • Python

    def foo(a, b, c, *args):
      pass
    
  • JS

    function foo(a, b, c, ...args) {
      var a, b, c;
      [a, b, c, ...args] = args;
    }
    

Argument Unpacking

Python does not support unpacking in the argument list but you can still unpack the list in the function.

  • Python
    def foo(*args):
      a, b, c, *args = args
    
  • JS
    function foo([a, b, c, ...args] = []) {}
    

Unpacking Keyword Arguments

Javascript does not support keyword arguments but you can still unpack a single object and use the keys similarly to keyword arguments.

  • Python

    def foo(a, b, c):
      pass
    
    foo(**{"a": 1, "b": 2, "c": 3})
    
  • JS

    function foo({ a, b, c }) {}
    foo({ a: 1, b: 2, c: 3 });
    

If you can think of more examples or saw an error feel free to let me know via Twitter or open an issue on Github.


  1. From Functional Light JS by Kyle Simpson.