Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Last active June 28, 2023 08:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juancarlospaco/a219aa0240a7f798b3f3fbe9682dd6a6 to your computer and use it in GitHub Desktop.
Save juancarlospaco/a219aa0240a7f798b3f3fbe9682dd6a6 to your computer and use it in GitHub Desktop.
Cython Cheatsheet

Python 3 to Cython CheatSheet

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.

Integers

Python (Medium Integers)

a = 1

Cython (Medium Integers)

cdef int a
a = 1

Nim + NimPy (Medium Integers)

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

Nim + NimPy (Big Integers)

var a = 1000000000000000000

Nim can make it explicit int64() if you want, but is not needed.

Strings

Python

a = "foo"

Cython

cdef char* a
a = "foo"

Nim + NimPy

var a = "foo"

Floats

Python (Small Floats)

a = 1.0

Cython (Small Floats)

cdef double a
a = 1.0

Nim + NimPy (Small Floats)

var a = 1.0

Python (Medium Floats)

a = 1.000001

Cython (Medium Floats)

cdef long double a
a = 1.000001

Nim + NimPy (Medium Floats)

var a = 1.000001

Python (Big Floats)

a = 1.0000000001

Cython (Big Floats)

cdef longlong double a
a = 1.0000000001

Nim + NimPy (Big Floats)

var a = 1.0000000001

Booleans

Python

a = True

Cython

cdef bint a
a = True

Nim + NimPy

var a = true

Functions

Python

def foo(bar: str, baz: bool) -> int:
    return 1

Cython

cdef int foo(char bar, bint baz)

Nim + NimPy

proc foo(bar: string, baz: bool): int =
    return 1

Methods

Python

def foo(self, bar: str, baz: bool) -> int:
    return 1

Cython

cdef int foo(self, char bar, bint baz)

Nim + NimPy

proc foo(self: ObjectHere, bar: string, baz: bool): int =
    return 1

Lambdas

Python

from typing import Callable

variable: Callable[[int, int], int] = lambda var1, var2: var1 + var2

Nim + NimPy

( 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.

Classes

Python

class MyObject(object):
  """ Documentation String """
  pass

Cython

Nim + NimPy

type MyObject = object  ## Documentation String

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, but Cython itself dont take care about them, you need third party tool """

Nim + NimPy

# 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.

@AndrejHatzi
Copy link

What about classes?

@juancarlospaco
Copy link
Author

  • 2019 Update: Added Nim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment