Skip to main content
Shed Skin translates Python code into C++, which requires all variables to have a single, static type throughout their lifetime. This is the most fundamental restriction when working with Shed Skin.

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:
Type changes will cause compilation errors. Plan your variable usage carefully to avoid type conflicts.

Abstract types and inheritance

While variables must have a static type, that type can be abstract. This allows for polymorphism through inheritance:
This works because both 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

The None 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):
When None becomes a NULL pointer in C++, using it with scalars can lead to undefined behavior. Use sentinel values instead:

Segfault risks

Since None translates to a NULL pointer in C++, dereferencing it will cause a segmentation fault:
Always check for None before dereferencing:

Integer and float mixing

Unlike most type combinations, integers and floats can usually be mixed together:
When integers and floats are mixed, the integers are automatically converted to floats.
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:
The type inference analyzes your entire program to determine types, but it can only succeed if the typing rules are followed consistently.
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.