Implicit static typing
Unlike standard Python where variables can change types dynamically, Shed Skin requires that each variable maintains the same type from assignment to usage. The compiler uses type inference to automatically determine these types, but they must remain consistent.Single type per variable
Variables can only ever have one type. Once assigned, a variable cannot be reassigned to a value of a different type:Abstract types and inheritance
While variables must have a static type, that type can be abstract. This allows for polymorphism through inheritance:Dog and Cat inherit from Animal, making them compatible for assignment to the same variable.
Collection type restrictions
Collections like lists, tuples, sets, and dictionaries must contain elements of a single, consistent type.Homogeneous collections
All elements in a collection must share the same type:Empty collections in larger structures are fine, as shown in the
c example above where one inner list is empty.Dictionary restrictions
Dictionaries can have different types for keys and values, but all keys must share one type and all values must share one type:Special case: Tuples of length 2
Shed Skin makes a special exception for 2-element tuples, which can contain mixed types:This exception exists to support common patterns like dictionary iteration with
items(). Longer tuples still require homogeneous types.None mixing rules
TheNone value has special restrictions based on the types it’s mixed with.
Allowed with non-scalar types
None can be mixed with non-scalar types (objects, lists, dictionaries, etc.):
Not allowed with scalar types
None cannot be mixed with scalar types (int, float, bool, complex):
Segfault risks
SinceNone translates to a NULL pointer in C++, dereferencing it will cause a segmentation fault:
None before dereferencing:
Integer and float mixing
Unlike most type combinations, integers and floats can usually be mixed together:Shed Skin will generate an error in specific cases where int/float mixing cannot be supported. If you encounter such an error, separate your integer and float operations.
Type inference in practice
Shed Skin uses sophisticated type inference to determine variable types automatically:Advanced: Why these restrictions?
Advanced: Why these restrictions?
C++ is a statically-typed language, meaning all types must be known at compile time. Shed Skin’s type inference determines these types automatically, but once determined, they become part of the generated C++ code.When you write
a = 1, Shed Skin generates C++ code declaring a as an integer. If you later write a = "hello", there’s no way to change that C++ declaration—the variable has already been typed.These restrictions enable Shed Skin to generate efficient C++ code that runs much faster than interpreted Python, at the cost of some flexibility.