フィールドと__init__メソッド
structの状態を初期化します
「フィールドと__init__メソッド」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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 = 0The 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 = 0Why 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 = ySet 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 = yDefault 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 = 0The 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 = yQuick 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. ✨
よくある質問
「フィールドと__init__メソッド」レッスンは無料ですか?
はい。「フィールドと__init__メソッド」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「フィールドと__init__メソッド」で何を学びますか?
structの状態を初期化します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「フィールドと__init__メソッド」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- structの定義
- フィールドと__init__メソッド
- Structへのメソッド追加
- StructとPythonクラスの違い