constructed startin with 1x1mm square; last square is 144x144
Published on

Three Python Features I Would Love To Have In JavaScript

Authors

As someone who primarily learned to code using JavaScript, reading languages like C and Java wasn’t too much of a struggle once I learned to read the typing-related code (something that became all the more easy after adopting TypeScript). But once I started digging deeper into machine learning and data science it became clear I would not be able to avoid learning Python. I was reluctant to learn it primarily because its syntax is so different from that of JavaScript (whitespace?!), and I was unmoved by people and comics singing the language’s praises (well, maybe I was slightly moved by the comics).

But I eventually acquiesced and once I got comfortable reading and writing Python I discovered some things I actually enjoyed about the language. In fact these were things I wish I could adopt into my JavaScript code. Below is a short list of these features:

1. Slicing Notation

This was probably the first part of Python’s syntax that made me react with “Okay, that’s a pretty nice feature.” Python’s slicing syntax gives you the ability to easily get multiple subsections of any list (i.e., “array” in JavaScript). It looks like the following:

example_list = [1, 2, 3, 4]
example_list[:1] # -> [1]
example_list[1:] # -> [2, 3, 4]
example_list[1:3] # -> [2, 3]

As you can see, the slice notation syntax can be thought of as consisting of optional "start" and "stop" values like so [start:stop] (Technically both the "start" and "stop" values can be optional because using [:] will return a complete copy of the original list), but you can also use a "step" property to skip values within the sliced subsets like so:

example_list[0:4:2] # -> [1, 3]

2. List Comprehensions

List comprehensions are a prime example of one of the original goals of Python: to make code read more like a human language (at least if you’re an English speaker!). Comprehensions give you a simple way to create new lists using this basic syntax:

[expression for value in iterable]

Here is a simple example:

doubled_list = [i * 2 for i in [1,2,3]]
doubled_list # -> [2, 4, 6]

You can even create nested comprehensions, which are especially useful when dealing with nested data structures such as matrices:

original_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed_matrix = [[row[i] for row in original_matrix] for i in range(3)]
transposed_matrix # [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Unfortunately, although list comprehensions were actually on the roadmap for ECMAScript 2015 and even implemented in some versions of Firefox, the feature was later removed: Array comprehensions | MDN (12/2021 update: comprehensions are being reconsidered for ECMAScript 7!).

3. Named Parameters

Python gives you the ability to both pass arguments to a method in a set order, just like you would in JavaScript, or create named parameters that are defined with a default value and that can then be passed in any order. Let’s look at an example:

def rectangle_area(width=0, height=0):
    return width * height

I could call this method in any of the following ways:

rectangle_area(5, 7)
rectangle_area(width=5, height=7)
rectangle_area(height=7, width=5)

You can sort of adopt this pattern in JavaScript methods using objects:

function rectangleArea(rectangleObj) {
  return rectangleObj.width * rectangleObj.height
}
rectangleArea({ width: w, height: h })
rectangleArea({ height: h, width: w })

But I personally find the Python pattern easier to read at a glance, especially for methods with more than three or four arguments which are common in several data science and machine learning libraries.

If you’re a developer who has worked with JavaScript but never with Python, hopefully this gives you a small taste of some of the things Python does well. Some features such as the mandatory whitespace and lack of keywords for variable declarations still look a little strange when I step back into Python after working in JavaScript for a while, but I am enjoying working with these features and many other methods in the standard library on a daily basis.