Fields and the __init__ Method
Initialize a struct's state.
Fields Start Empty
Declaring fields gives a struct its shape, but it does not fill them in. You still need to set each field's starting value. 🧱
Meet __init__
The special method __init__ runs when you create an instance. Its job is to set up the struct's fields with real values.
fn __init__(out self):
self.x = 0
self.y = 0All lessons in this course
- Defining a struct
- Fields and the __init__ Method
- Adding Methods to a Struct
- Structs vs Python Classes