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
transpiles to:
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:
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:
transpiles to:
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:
transpiles to:
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