Questions Columns Rows
GitHub icon

Cython

Cython - Programming language

< >

Cython is an open source programming language created in 2007.

#250on PLDB 16Years Old 698Repos

Cython is a superset of the Python programming language, designed to give C-like performance with code which is mostly written in Python. Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement. Read more on Wikipedia...


Example from Wikipedia:
In [1]: %load_ext Cython In [2]: %%cython ...: def f(n): ...: a = 0 ...: for i in range(n): ...: a += i ...: return a ...: ...: cpdef g(int n): ...: cdef int a = 0, i ...: for i in range(n): ...: a += i ...: return a ...: In [3]: %timeit f(1000000) 42.7 ms ± 783 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [4]: %timeit g(1000000) 74 µs ± 16.6 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Language features

Feature Supported Token Example
Integers
Floats
Hexadecimals
Octals
Scientific Notation
Binary Literals
# 0[bB](?:_?[01])+
Functions
While Loops
Case Sensitivity
Print() Debugging
Threads
Line Comments
# A comment
Dispose Blocks Pattern
with resource_context_manager() as resource:
   # Perform actions with the resource.
# Perform other actions where the resource is guaranteed to be deallocated.
Zero-based numbering
Strings
"hello world"
Inheritance
class SumComputer(object):
   def __init__(self, a, b):
       self.a = a
       self.b = b
   def transform(self, x):
       raise NotImplementedError
   def inputs(self):
       return range(self.a, self.b)
   def compute(self):
       return sum(self.transform(value) for value in self.inputs())
 class SquareSumComputer(SumComputer):
     def transform(self, x):
         return x * x
 class CubeSumComputer(SumComputer):
     def transform(self, x):
         return x * x * x
Semantic Indentation
class Person (object):
  def __init__(self, name):
    self.name = name
Operator Overloading
# Python Program illustrate how  
# to overload an binary + operator 
  
class A: 
    def __init__(self, a): 
        self.a = a 
  
    # adding two objects  
    def __add__(self, o): 
        return self.a + o.a  
ob1 = A(1) 
ob2 = A(2) 
ob3 = A("Geeks") 
ob4 = A("For") 
  
print(ob1 + ob2) 
print(ob3 + ob4)
Multiple Inheritance
class Base1:
    pass
class Base2:
    pass
class MultiDerived(Base1, Base2):
    pass
# Or multilevel inheritance:
class Base:
    pass
class Derived1(Base):
    pass
class Derived2(Derived1):
    pass
Lists
myList = [1, 2, 3, 4, 5]
Multiline Strings
template = """This is the first line.
This is the second line.
This is the third line."""
Mixins
# https://easyaspython.com/mixins-for-fun-and-profit-cb9962760556
class EssentialFunctioner(LoggerMixin, object):
Iterators
https://www.w3schools.com/python/python_iterators.asp
Infix Notation
seven = 3 + 4
File Imports
import datetime
oTime = datetime.datetime.now()
from my_pkg import my_funcs
Assignment
name = "John"
Directives
from __future__ import feature
# coding=
Generators
https://wiki.python.org/moin/Generators
Constructors
MultiLine Comments
''' A comment.
'''
Comments
# This is a comment
Conditionals
if True:
 print("Hello world")
Classes
class Person (object):
  def __init__(self, name):
    self.name = name
Booleans
Dynamic Properties
class Person (object):
  def __init__(self, name):
   self.name = name

person = Person("John")
person.age = 50
Symbol Tables
# https://eli.thegreenplace.net/2010/09/18/python-internals-symbol-tables-part-1
Shebang
#!/usr/bin/env python
Bitwise Operators
x << y
Ternary operators
print(1 if 1 else 0)
Single Dispatch
Duck Typing
Disk Output
with open('helloworld.txt', 'w') as filehandle:
 filehandle.write('Hello, world!\n')
Units of Measure X
Pointers X
Case Insensitive Identifiers X
Regular Expression Syntax Sugar X
Enums X
# Though there is an Enum class in stdlib https://peps.python.org/pep-0435/
Macros X
Variable Substitution Syntax X

View source

- Build the next great programming language · Search · Add Language · Features · Creators · Resources · About · Blog · Acknowledgements · Stats · Sponsor · Traffic · Traffic Today · Day 279 · feedback@pldb.com · Logout