Python 3.7+ to Cython CheatSheet by examples as simple as posible, because I cant find a Cython Cheatsheet on Internet.
It start with simple stuff and continues towards more complex ones, is compatible with PXD that allows to left the *.py
untouched.
All values and variable names are example values.
Python (Medium Integers)
a = 1
Cython (Medium Integers)
cdef int a
a = 1
var a = 1
Nim can make it explicit int32()
if you want, but is not needed.
Python (Big Integers)
a = 1000000000000000000
Cython (Big Integers)
cdef longlong a
a = 1000000000000000000
var a = 1000000000000000000
Nim can make it explicit int64()
if you want, but is not needed.
Python
a = "foo"
Cython
cdef char* a
a = "foo"
var a = "foo"
Python (Small Floats)
a = 1.0
Cython (Small Floats)
cdef double a
a = 1.0
var a = 1.0
Python (Medium Floats)
a = 1.000001
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
Python
a = True
Cython
cdef bint a
a = True
var a = true
Python
def foo(bar: str, baz: bool) -> int:
return 1
Cython
cdef int foo(char bar, bint baz)
proc foo(bar: string, baz: bool): int =
return 1
Python
def foo(self, bar: str, baz: bool) -> int:
return 1
Cython
cdef int foo(self, char bar, bint baz)
proc foo(self: ObjectHere, bar: string, baz: bool): int =
return 1
Python
from typing import Callable
variable: Callable[[int, int], int] = lambda var1, var2: var1 + var2
( proc(var1, var2: int): int = var1 + var2 )
Python Lambdas are not supported on Cython. Documentation is missing on Cython.
For Python you are forced to put it on a variable for Type Annotations, Nim anonymous functions are Typed by itself.
Python Lambda syntax is different, Nim anonymous functions are just functions without name.
Python
class MyObject(object):
""" Documentation String """
pass
Cython
type MyObject = object ## Documentation String
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, but Cython itself dont take care about them, you need third party tool """
# This is a comment
## This is a Documentation Comment, **MarkDown** or *ReSTructuredText*, can generate HTML, LaTex, JSON documentation by itself, no third party tool required
Nimpy puts Documentation Comments on the __doc__
as expected.
What about classes?