0Pricing
Mojo Academy · Lesson

Fields and the __init__ Method

Initialize a struct's state.

Fields and the __init__ Method is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 = 0

The self Parameter

Inside __init__, self refers to the new instance being built. You assign to self.field to give that instance its data.

self.x = 0

Why out self

Writing out self tells Mojo this method is constructing the value, so self starts uninitialized and you must fill every field.

fn __init__(out self):

Taking Arguments

An initializer usually takes arguments so each instance can start with different data passed in by the caller.

fn __init__(out self, x: Int, y: Int):
    self.x = x
    self.y = y

Set Every Field

You must assign every field in __init__. Mojo will not let a struct exist with some fields left uninitialized.

Creating an Instance

To build a value, call the struct like a function. Mojo runs __init__ and hands you back a ready instance.

var p = Point(3, 4)

Reading a Field

Once built, reach a field with dot access. So p.x reads the x you stored when the instance was created.

print(p.x, p.y)

Arguments Map to Fields

A common pattern is naming arguments like the fields and assigning each across. This keeps the initializer easy to read.

self.x = x
self.y = y

Default Starting Values

You can give fields sensible defaults by assigning constants in __init__ when no useful argument is provided by the caller.

fn __init__(out self):
    self.x = 0
    self.y = 0

The Full Picture

Fields declare what data exists; __init__ decides its starting value. Together they turn a blueprint into a usable instance.

struct Point:
    var x: Int
    var y: Int
    fn __init__(out self, x: Int, y: Int):
        self.x = x
        self.y = y

Quick Check

Let us confirm how initialization works.

Recap

You used __init__ to set up fields when an instance is born. Next you will add methods so your type can act, not just hold data. ✨

Frequently asked questions

Is the “Fields and the __init__ Method” lesson free?

Yes — the full text of “Fields and the __init__ Method” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fields and the __init__ Method”?

Initialize a struct's state. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fields and the __init__ Method” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining a struct
  2. Fields and the __init__ Method
  3. Adding Methods to a Struct
  4. Structs vs Python Classes
← Back to Mojo Academy