Juan Carlos
Posted on October 2, 2019
Who
- Python 3.7+ š
- Cython 0.2x Ā©ļø (Not V1 yet)
- Nim 1.0 š
Its meant to compare friendliness of writing and easy reading.
No performance benchmark.
Wat
Python 3 to Cython CheatSheet, with short examples, as simple as posible,
because I cant find a Cython Cheatsheet on Internet. It also adds Nim.
It start with simple stuff and continues towards more complex ones.
All values and variable names are example values.
Cython and Nim are alternatives to make Python faster (Compiled) and safer (Strong Static Typing).
CheatSheet: Python, Cython, Nim
Small Integers
- Python dont have Types for Medium Integers nor Small Integers.
Cython (Medium Integers)
cdef int a
a = 1
var a = 1
Big Integers
Python
a = 100000000
Cython
cdef longlong a
a = 100000000
var a = 100000000
- Nim optionally can use explicit
int8()
,int16()
,int32()
,int64()
,int()
.
Unsigned Integers
- Python doesnt have Types for Positive Integers nor Unsigned Integers.
Cython (Unsigned Positive Integers)
ctypedef unsigned a
a = 1
var a = Positive(1)
- Nim has
Positive()
starting at1
andNatural()
starting at0
. - Nim optionally can use explicit
uint8()
,uint16()
,uint32()
,uint64()
,uint()
. - Cython doesnt have Types for non-Zero values.
Strings
Python
a = "foo"
Cython
cdef char* a
a = "foo"
var a = "foo"
F-Strings
Python
a = f"foo {variable} {1 + 2} bar"
- Cython doesnt have F-Strings Types.
import strformat
var a = fmt"foo {variable} {1 + 2} bar"
- Nim needs
import strformat
, they can be compile-time.
Small Floats
- Python doesnt have Types for Small Floats.
Cython (Small Floats)
cdef double a
a = 1.0
var a = 1.0
Big Floats
- Python doesnt have Types for Medium Floats.
Cython (Medium Floats)
cdef long double a
a = 1.000001
var a = 1.000001
Python (Big Floats)
a = 1.0000000001
Cython (Big Floats)
cdef longlong double a
a = 1.0000000001
var a = 1.0000000001
- Nim optionally can use explicit
float32()
,float64()
,float()
.
Booleans
Python
a = True
Cython
cdef bint a
a = True
var a = true
- Nim optionally can use the alias
on
forTrue
,off
forFalse
.
Functions
Python
def foo(bar: str, baz: bool) -> int:
return 1
Cython
cdef int foo(char bar, bint baz)
def foo(bar: str, baz: bool) -> int:
return 1
proc foo(bar: string, baz: bool): int =
return 1
- Nim optionally can use
auto
for return Type, it will inference the return Type. - Nim optionally can use
any
for argument Type, it will inference the argument Type. - On Nim arguments of a function are immutable by default.
- Nim optionally can use
func
instead ofproc
for pure-functions Functional Programming (Side-Effect Free).
Methods
Python
def foo(self, bar: str, baz: bool) -> int:
return 1
Cython
cdef int foo(self, char bar, bint baz)
def foo(self, bar: str, baz: bool) -> int:
return 1
proc foo(self: ObjectHere, bar: string, baz: bool): int =
return 1
-
ObjectHere
added for clarity.
Inlined Functions
- Python doesnt have Inlined Functions.
Cython
cdef inline int foo(char bar, bint baz)
def foo(bar: str, baz: bool) -> int:
return 1
- Cython adds
inline
oncdef
to inline a function.
proc foo(bar: string, baz: bool): int {.inline.} =
return 1
- Nim adds
{.inline.}
Pragma to inline a function.
Lambdas
Python
from typing import Callable
variable: Callable[[int, int], int] = lambda var1, var2: var1 + var2
( proc (var1, var2: int): int = var1 + var2 )
- Python you are forced to put it on a variable for Type Annotations, Nim anonymous functions are Typed by itself.
- Python Lambdas are not supported on Cython.
- Python Lambda syntax is very different from function.
- Nim anonymous functions are just functions without name.
- Nim anonymous functions optionally can be
func
for pure-functions Functional Programming (Side-Effect Free). - Parens added for clarity.
Classes
Python
class MyObject(object):
""" Documentation Here, plain-text by default """
pass
type MyObject = object ## Documentation Here, **MarkDown** or *ReSTructuredText*
Comments & DocStrings
Python
# This is a comment
""" This is a DocString, plain-text by default """
Cython
# This is a comment
""" This is a DocString, plain-text by default """
# This is a comment
## This is a Documentation Comment, **MarkDown** or *ReSTructuredText*
- Nim can generate HTML, LaTex, JSON documentation by itself, no third party tool required.
-
NimPy puts Documentation Comments on the
__doc__
as expected.
JSON
Python
import json
variable = json.loads("""{
"key": "value",
"other": true
}
""")
- Cython doesnt have JSON Types.
import json
var variable = %*{
"key": "value",
"other": true
}
- Nim uses Literal JSON as-is directly on the code, not a multi-line string.
SubRanges
- Python doesnt have SubRange Types.
- Cython doesnt have SubRange Types.
var subrange_integer: range[1..3] = 2
var subrange_float: range[1.0..3.0] = 2.5
var subrange_negative_integer: range[-3..-1] = -2
var subrange_negative_float: range[-3.0..-1.0] = -2.5
-
subrange_integer
only takes values from1
to3
. -
subrange_float
only takes values from1.0
to3.0
. -
subrange_negative_integer
only takes values from-3
to-1
. -
subrange_negative_float
only takes values from-3.0
to-1.0
.
Static Constants
- Python doesnt have Static Constants (Wont compile).
- Cython doesnt have Static Constants (or is recommended to not use them?).
const static_constant = 2 + 2
-
static_constant
is immutable, compile-time pre-computed value.
Immutables
- Python doesnt have run-time immutable variables.
- Cython doesnt have run-time immutable variables.
const foo = "Compile-Time Immutable Constant"
let bar = "Run-Time Immutable Variable"
var baz = "Run-Time Mutable Variable"
Compile-Time Function Execution
- Python doesnt have Compile-Time Function Execution (Wont compile).
- Cython doesnt have Compile-Time Function Execution.
let compile_time_sum = static(1 + 2)
static:
assert 4 > 2, "Compile-Time Assert"
const compile_time_config = static_read("config.ini")
- Everything you put inside
static:
orstatic()
will be run at compile-time.
Miscelaneous
File Read Write
Python / Cython
with open("file.txt", "r") as infile:
content = infile.read()
with open("file.txt", "w") as outfile:
outfile.write("some data")
var content = read_file("file.txt")
write_file("file.txt", "some data")
- Cython even if its compiled can not read files at compile-time, but Nim
static_read()
can.
const content = static_read("file.txt")
- Nim
static_read()
is a compile-timeread_file()
, so the Cython version is run-time while the Nim version is compile-time.
Module Self-Executions
Python / Cython
if __name__ in "__main__":
when is_main_module:
- Nim
when
is a compile-timeif
, so the Cython version is run-time while the Nim version is compile-time.
Compile-time help messages
- Cython doesnt have a way to print compile-time help messages for users.
{.hint: "This is a Hint, printed at Compile-Time, with colors too" .}
{.warning: "Warning Pragma, is Compile-Time, produces no extra code" .}
Thank you for playing
- Learn Nim today.
- Learn Cython.
- Play with Nim on a sanbox directly from browser.
- Cython Tutorial.
- Say Hi on the bridged Gitter, Matrix, Telegram, IRC, Discord.
š
Posted on October 2, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.