kcl-langPython-to-KCL Cheat Sheet

Python-to-KCL Cheat Sheet

FeaturePythonKCL
Variable AssignmentCan be reassigned later.
x = 42
Cannot be reassigned once declared.
x = 42
Data TypesNumbers, strings, booleans, lists, tuples, dicts, objects, NoneNumbers (with units), strings, booleans, arrays, objects
ImmutabilityVariables can be reassigned. Lists, dicts, and objects are mutable.Variables cannot be reassigned. Arrays and objects are immutable.
Units of MeasureNot supported.12in
35mm
1m + 1ft Units are automatically converted.
cos(45deg) Function parameters automatically convert units.
arr[5_] Unitless numbers like array indexes use the Count unit, using the underscore suffix.
Conditionalsif/else is a statement.
if x < 0:
do_negative()
elif x < 10:
do_less_than_ten()
else:
do_ten_or_greater()
if/else is an expression that returns a result. if expressions require a matching else.
result = if x < 0 {
doNegative()
} else if x < 10 {
doLessThanTen()
} else {
doTenOrGreater()
}
Functions with Named ParametersWhen calling a function, parameters can be named or positional.
def make_vector(x, y):
return [x, y]

make_vector(10, 5)
make_vector(x = 10, y = 5)
When calling a function, parameters must be named.
fn makeVector(x, y) {
return [x, y]
}

makeVector(x = 10, y = 5)
Functions with Positional-Only ParametersSome parameters can be declared as positional-only.
def make_vector(x, y, /):
return [x, y]

make_vector(10, 5)
The first, and only first, parameter can optionally be declared with an @ prefix, meaning that it's unnamed and positional-only. This allows it to work with pipelines. See below.
fn makeVector(@x, y) {
return [x, y]
}

makeVector(10, y = 5)
Anonymous Functionslambda x: x * xfn(@x) { return x * x }
PipelinesNot supported. You must use nested function calls like g(f(x)). Sometimes you can use method chaining like x.f().g() if the object supports it.The value on the left of the |> is substituted for the first positional-only parameter on the right.
x |> f() |> g()
Rangesrange(5)
range(2, 5)
No support for end-inclusive ranges.
range(0, 10, 2)
[0 ..< 5]
[2 ..< 5]
[1 .. 5] Starts with 1 and includes 5.
Step other than 1 is not supported.
Map a Collectionlist(map(lambda x: 2 * x, arr))
OR
[2 * x for x in arr]
map(arr, f = fn(@x) { return 2 * x })
Reduce a Collection
result = 0
for item in arr:
result += 2 * item
OR
from functools import reduce
result = reduce(lambda sum, item: sum + 2 * item, arr, 0)
The accumulator parameter must be named accum.
result = reduce(arr,
initial = 0,
f = fn(@item, accum) { return accum + 2 * item })
Array Concatenation[1, 2, 3] + [4, 5, 6]concat([1, 2, 3], items = [4, 5, 6])
Raise to a Power2**52^5
OR
pow(2, exp = 5)
Vector Addition
a = [1, 2, 3]
b = [4, 5, 6]
result = [x + y for x, y in zip(a, b)]
OR
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a + b
a = [1, 2, 3]
b = [4, 5, 6]
result = vector::add(a, v = b)
Logical OperatorsShort circuits.
a and b
a or b
not a
Does not short circuit. See docs.
a & b
a | b
!a
Assertionassert(my_boolean)
assert(x == 42)
assert(x > 0)
assertIs(myBoolean)
Numbers should use the special assert() function with the correct parameters so that the error message can include the actual numeric value.
assert(x, isEqualTo = 42)
assert(x, isGreaterThan = 0)
Exceptions
try:
raise Exception("Something unexpected")
except Exception as e:
print(e)
Not supported.
Modules/Importsimport module
import module as alias
from module import x, y
import "file.kcl"
import "file.kcl" as alias
import x, y from "file.kcl"
Supports other CAD format imports. See docs.
import "file.obj"
CAD PrimitivesNot built in. Use external libs.startSketchOn(...), line(...), elliptic(...), extrude(...), revolve(), fillet(...), patternCircular3d(...), union(...), and many more.
Found a typo?