DEV Community

Cover image for Exotic Features of Python3
Juan Carlos
Juan Carlos

Posted on

Exotic Features of Python3

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

Enter fullscreen mode Exit fullscreen mode

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
           )

Enter fullscreen mode Exit fullscreen mode

Alt Text

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

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 1.4,
because I was migrating source code from Python to Nim.

For Python

Nim can be used to do Python, is an alternative to Cython,
but with more Pythonic syntax, and more features.

Alt Text

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.
  • Documentation generator with Markdown/RST doc comments.
  • Unittests runner, inline runnable example unittests.
  • Package manager.
  • Code style formatter.
  • App installer generator.
  • "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

Thank you for reading

There's bridged Gitter, Matrix, Telegram, IRC, Discord for Nim, come say Hello.

Telegram en Español.
👑

Top comments (0)