Skip to main content


You can add the @staticmethod decorator on a struct method to declare a static method.

For example:

from tensor import Tensor
from pathlib import Path

struct MyStruct:
var data: Tensor[DType.int8]

fn __init__(inout self): = Tensor[DType.int8]()

fn __moveinit__(inout self, owned existing: Self): = ^

fn load_from_file(file_path: Path) raises -> Self:
var new_struct = MyStruct() = file_path.read_bytes()
return new_struct ^

Unlike an instance method, a static method doesn't take an implicit self argument. It's not attached to a specific instance of a struct, so it can't access instance data.

For more information see the documentation on static methods.