Exotic Features of Python3
Tags: python, nim
Published: 2020-10-30 15:35:33

---
title: Exotic Features of Python3
published: true
description: Exotic Features of Python3?
tags: python, nim
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/7ekcd9ceqdd0427bzsdb.png
---

# Python

```python
import sys, math 

assert False ** False == 1

assert False == False in [False]

assert type(1 ** 1) != type(1 ** -1)

# print(f"""{ "yep\nope" }""")  # ERROR?

assert round(0.5) == 0.0
assert round(1.5) == 2.0
assert round(2.5) == 2.0

y = (1 << 53) + 1
assert y + 1.0 < y

o = []
for i in range(99999): o.append(False)
assert sys.getsizeof(o) == 824456 or sys.getsizeof(o) == 824472
assert sys.getsizeof(False) == 24

x = []
x.append(x)
assert x[0][0][0][0][0][0][0][0][0][0][0] == x
assert x[-1][-1][-1][-1][-1][-1][-1][-1] == x

#anon = lambda x:          ERROR?   
#           if x > 0:
#               return 1
#           else: 
#               return 0

```

# Nim

```nim
import math, strformat

assert not compiles(false ^ false == 1)

assert not(false == false in [false])

assert type(1 ^ 1) is type(1 ^ -1)

echo fmt"""{ "yep\nope" }"""

assert round(0.5) == 1.0
assert round(1.5) == 2.0
assert round(2.5) == 3.0

var y = (1 shl 53) + 1
assert not compiles(y + 1.0 < y)

var o: seq[bool]
for i in 0..99999: o.add false
assert sizeOf(o) == 8
assert sizeOf(false) == 1

var x: seq[int]
x.add(x)
assert not compiles(x[0][0][0][0][0][0][0][0][0][0][0] == x)
assert not compiles(x[^1][^1][^1][^1][^1][^1][^1][^1] == x)

var anon = (func (x: int): int =
             if x > 0:
               return 1
             else: 
               return 0
           )

```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6s40ohwgnt5zou9lq5n0.png)

Some occasional weird _"exotic features"_ found in the wild.

- [Play with the Nim code.](https://play.nim-lang.org/#ix=2Cuu)
- [Play with the Python code.](https://tio.run/##fVHLbsIwELz7K7bhEtMSJQH6iEh7aq@VuNIcXOFAJLAtx0h2q357uk4gUFRY2dJ6Zzyzaytn1lKMm6baKqkN1K6@gy0za0JYXXOsvLFNzWE43Cd5DskZhqUuqQQs2qzoGcYpHib@ekLhJj85jxJKyACUroQJyyAIviFwXH0IqXgAP1igAAN4nc/f5y@9npY7sQzjaEq9bRzFf4FkD6TnQHoCEAc5YBOzGUzHFG5xoD3X@UMUwwwcIRJZi4KUUkPlR9NMrHj45INmICOmFEfldmB6UMD3i1bc1NUXl2UoW8/HdDKZ3gPqXEIf0v/vd9pt3xNCbNeQPTjb3tUu4uL68hr2SB8lxbXdscmACSnQdMO2n0sGNoM@um/BBL/wGFUJFp4hzv5UfWhudlrgS58CHKfL4BI3bppf)

Code here is reduced to the minimal that still reproduces the bug,
but sometimes finding it on big code bases is not easy.

The examples use Python `3.8` and [Nim](http://nim-lang.org) `1.4`,
because I was migrating source code from Python to [Nim](http://nim-lang.org).


# For Python

[Nim](http://nim-lang.org) [can be used to do Python](https://github.com/juancarlospaco/faster-than-requests#use), is an alternative to Cython,
but with [more Pythonic syntax](https://github.com/Yardanico/nimpylib#usage), and more features.

[![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3i6nb9my6swplbyr2h08.png)](https://github.com/Pebaz/nimporter#possible-benefits)

### Nim builtin features:

- Write easy as Python, runs fast as Rust.
- Runs on Backend and Frontend.
- No GIL, no virtual machine.
- Automatic deterministic memory management with No Garbage Collector.
- Strong inferred static typing, no hints required.
- Complete online HTML documentation with search.
- No Dependencies, no need to setup virtual environment.
- Side-effects free functions.
- Multi-line multi-expression fast anonymous functions.
- Arrow functions.
- Immutability.
- [UFCS](https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax#Nim_programming_language).
- [Documentation generator with Markdown/RST doc comments.](https://nim-lang.github.io/Nim/docgen.html)
- [Unittests runner, inline runnable example unittests.](https://nim-lang.github.io/Nim/testament.html)
- Package manager.
- Code style formatter.
- [App installer generator.](https://nim-lang.github.io/Nim/niminst.html)
- "import" C/C++/JavaScript libraries.
- Runs on Arduino and Raspberry Pi.
- Proof engine with preconditions and postconditions (Ada like).
- Tiny native binaries, fast compilation.
- Easy to learn for Python/Ruby/D/Go developers.
- Not too verbose, similar to Python on verbosity.

# Learn more

- [**Nim for Python Developers.**](https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers#table-of-contents)


# Thank you for reading

[![](https://thepracticaldev.s3.amazonaws.com/i/4huq62a5r8tck64apdtd.png)](https://nim-lang.org)

There's bridged [Gitter](https://gitter.im/nim-lang/Nim), [Matrix](https://matrix.to/#/#freenode_#nim:matrix.org), [Telegram](https://t.me/nim_lang), [IRC](irc://freenode.net/nim), [Discord](https://discord.gg/ezDFDw2) for [Nim](https://nim-lang.org), come say *Hello*.

[Telegram en Español.](https://t.me/NimArgentina)
👑