Skip to content

init method shorthand

basedpython lets a class declare its constructor with the bare keyword init instead of def __init__. parameters prefixed with let are auto-assigned to self.<name> at the top of the method body, so the constructor and the instance-attribute declarations share a single signature

class Point:
    init(self, let x: int, let y: int)

transpiles to:

class Point:
    def __init__(self, x: int, y: int):
        self.x: int = x
        self.y: int = y

scope

init(...) is only recognised inside a class body. at module scope init(...) is still an ordinary function call expression. the parser tracks class nesting so the keyword does not leak into module-level call sites

bodyless and body forms

both shapes are accepted:

class A:
    init(self, let a: int, b: str)
class B:
    init(self, a: int):
        self.b = str(a)

a bodyless init(...) is filled in with : ... when no let parameter produces a body line. body-bearing init(...) keeps the user's statements; let self-assignments are prepended ahead of them

let parameter modifier

let may appear on any positional, positional-only, keyword-only, *args, or **kwargs parameter inside init. each let parameter emits a self-assignment using the parameter's annotation (self.<name>: <ann> = <name>) or, if unannotated, a bare assignment (self.<name> = <name>)

a non-let parameter is just a parameter — no attribute is created for it

var and visibility modifiers

var is the mutable counterpart of let; on an init parameter it self-assigns identically. a visibility modifier — private or public — may precede let / var. private name-mangles the synthesised attribute to self.__name, while the parameter itself keeps its declared name:

class A:
    init(private var a: int)

transpiles to:

class A:
    def __init__(self, a: int):
        self.__a: int = a

a visibility modifier without let / var has no attribute to name, and any other modifier keyword (final, abstract, …) is meaningless in this position — both are reported as errors

implicit self

self may be omitted from the parameter list. it is implied, so it is injected into the generated def __init__ signature and let / var attributes and self references in the body resolve against it:

class A:
    init(let a: int)

transpiles to:

class A:
    def __init__(self, a: int):
        self.a: int = a

why

constructors with self.x = x; self.y = y boilerplate are ubiquitous. collapsing the parameter list and the attribute declarations into one line removes that duplication without depending on @dataclass semantics