Skip to content

CPython-Like Builtins


Implemented CPython-Like Built-ins

The following built-in functions work similarly to their equivalents in CPython; see the specific functions below for notes

abs(object)

Currently only implemented for int32's or objects convertible to int32's. The __abs__ attribute is not currently supported.

breakpoint()

Drops the user into an interactive SPy debugging session via spdb, a pdb-like debugging interface for SPy.

dict[keytype, valuetype]()

In SPy, dict must always be fully typed and used as dict[keytype, valuetype]., The syntax dict[keytype, valuetype]() can be used to create a new empty dict of the given types. The simpler syntax d: dict[keytype, valuetype] = {} can also be used.
Unlike CPython, this does not (currently) accept an Iterable to create a new dict from.
The implementation (in SPy) of dict can be viewed here.

dir(object)

Returns a list of object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes. dir(type) is not currently implemented.
The no-argument form of dir() (i.e. print local variables) is not currently implemented. Custom __dir__ methods on objects are not currently supported.

float(object)

Converts object to a float if able. float is an alias for the f64 type.

getattr(obj, name: str)

Return the value of the named attribute of object. attr must be blue

hash(object)

Currently implemented for types: i8,i32, u8, bool, str.
By default, instances of SPy structs are not hashable. As a planned future feature, structs will have auto-generated __hash__ by default, but this is awaiting implementation. Currently, users can implement the __hash__ function to permit hashing.

int(object)

Converts object to an int if able. Works for number types, as well as strings.
The int type is currently an alias to i32. In the future, int will alias preferred individual types for specific platforms, but currently it is always i32.

len(object)

Return the length (the number of items) in a container

list[type]()

The syntax list[type]() can be used to create a new empty list of the given type. The simpler syntax l: list[membertype] = [] can also be used. Unlike CPython, this does not (currently) accept an Iterable to create a new list from.
The implementation (in SPy) of list can be viewed here.

max(x: i32, y: i32)

Currently only implemented for int32's or objects convertible to int32's.

min(x: i32, y: i32)

Currently only implemented for int32's or objects convertible to int32's.

object

object is implemented as a type, and can be used as a parameter or return type. "Plain" objects (i.e. x = object()) are not supported.

print(obj)

The print function is currently not variadic, in the sense that it only accepts a single argument. The built-in types are special-cased, and SPy can always print blue objects by pre-computing their string representation

range(stop)

range(start, stop, step)

Creates an iterable set of indices between start and stop, jumping over step indices between each.

The implementation (in SPy) of range can be viewed here.

repr(object)

Returns string containing a printable representation of an object.

setattr(object, name: str, value: obj)

Assigns value to the attribute of object named by name. attr must be blue.

slice(stop)

slice(start, stop, step=None)

Return a slice object representing the items reached when iterating over range(start, stop, step). The start and step arguments default to None.

str(object)

Returns a string version of the object. Selecting an encoding is not currently implemented.

tuple()

The syntax tuple[t1, t2, ...](val1, val2 ...) can be used to create a new tuple, with t1 as the type of val1, etc. unlike CPython, this does not (currently) accept an Iterable to create a new tuple from.
The implementation (in SPy) of tuple can be viewed here.

type(object)

Returns the type (i.e. the dynamic type at runtime) of an object

Not-Implemented CPython Built-ins

The following CPython built-ins are not currently implemented in SPy. Each category has a brief note about the current state of that category of object or function - some require additional internal mechanics, others are simply lower priority that other facets of the language to this point.

Async

SPy does not currently have an async story.

aiter(), anext()

Iterables and Iterators

Iterables and collections are very much an area of active developmen; as their API solidifies, these types of builtins should become more straightforward to implement.

Generators are not currently supported in SPy.

all(), any(), enumerate(), filter(), iter(), map(), next(), reversed(), sorted(), sum(), zip()

Math

Number types beyond int and float are in active development; some of the math functions below are also in active development.

divmod(), bin(), hex(), oct(), pow(), round()

Function Types, Introspection and Metaprogramming

The internals of SPy are significantly different from CPython; as such, the road to (and need for) some of these built-ins is less straightforward. Some are also waiting on internal details to solidify prior to implementation.

callable(), classmethod(), compile(), delattr(), eval(), exec(), globals(), hasattr(), help(), id(), isinstance(), issubclass(), locals(), property(), super(), vars(). __import__()

Type Conversion

Many of these types are not implemented yet; others are in active development.

ascii(), bool(), bytearray(), bytes(), chr(), complex(), format(), frozenset(), memoryview(), ord() set()

I/O

The I/O story is currently a high priority and is in active development.

input(), open()