Nim - it feels like Python

Posted on Tue 02 May 2017 in Nim

In the Python world, there is a quest to always make Python "faster" by certain individuals/groups (sometimes due to need, sometimes simply to test the limits of the language and sometimes simply because we can).

There are many variations of this in the form of JIT(Just-In-Time) Compilers, as well as sub/supersets of the Python language itself.

Here is a quick list of some of the performance-enhancing tools written to make Python "faster":

  • PyPy (JIT)
  • Cython (superset)
  • Pyston (JIT)
  • Pyjion (JIT)
  • Nuitka (Compiler ~ of sorts)

There has even be talk of removing the GIL (Global Interpreter Lock) to improve Python's performance. While these initiatives are great and I admire the work done through these initiatives to make new (and quite likely, existing) codebases gain better performance, it may also be prudent to invest a little time evaluating languages that provide better-performance out-of-the-box.

There is a saying that: Go-lang was written to attract Java developers but many Python developers picked up the language instead

How far this is true, I am not sure. Go and Rust are the 2 trending systems-languages as of 2017. They get quite a bit of coverage (blogs, etc.) and have corporate-financial-backing.

On the topic of newer systems-languages though, I would like to introduce the language called: Nim/nim-lang (formerly called "Nimrod"): Nim-lang website

Nim can be summarized to be addressing issues at the same level as: C++, D, Go, Rust

Nim is mostly community-driven, with a very small group of core/compiler developers.

I am not an expert in programming-language design, so any opinions I have surrounding that topic will more than likely be incorrect. However, I would like to display some Nim code to show how similar it can otherwise appear to Python (and justify why the language appeals to me).

Here is a short snippet of Nim code that calculates whether a number is even:

import math
import strutils

proc is_even(value2: int): int =
    if value2 mod 2 == 0:
      var new_value: int = value2 div 2
      return new_value

    else:
      echo "The number you have entered is an odd number: "
      return value2

echo "Enter your Number: "
var val: string = readLine(stdin)
var val_cleaned: int = parseInt(val)
echo is_even(val_cleaned)

If you are coming from a purely Python/dynamic programming background, it may be a bit unusual to initially understand the code. However, let me rewrite the part where the proc is, which will then display how similar Nim feels to Python:

def is_even(value2):
    if value2 % 2 == 0:  
        new_value = value2 / 2  
        return new_value  
    else:
        print("The number you have entered is an odd number: ")
        return value2

I have previously dabbled in C, C++ and also attempted to learn Go. None of these languages were quite as enjoyable to code in (for a Pythonista familiar with dynamic-programming). After overcoming the hurdle of static-typing, which has become a very useful feature to have, Nim and Python feel very alike in terms of readability, etc.

The most critical thing I learnt when learning Nim was that: a proc in Nim is a def in Python

(in simpler terms: a function is a function)

I am now using Nim for a few things, chief among them being CLI-apps. The code is compiled to dependency-free binaries and (from the library I use), the apps execute similar to what most Linux-users would be used to (apt install something or tar -czf file.tar.gz folder/).

I have no plans on completely switching to Nim. The dearth of libraries/assistance/documentation/community is part of the reason why Python is such a great language.

However, if you are a frequent participant of tech-blogs/forums/news-sites and see lots of Go/Rust comparisons, I recommend considering Nim as well.

Some useful links/resources: