0Pricing
Mojo Academy · Ders

Alanlar ve __init__ Metodu

Bir struct'ın durumunu başlatın.

Alanlar ve __init__ Metodu, CoddyKit'te ücretsiz bir Mojo Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Mojo Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Mojo Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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. ✨

Sıkça Sorulan Sorular

“Alanlar ve __init__ Metodu” dersi ücretsiz mi?

Evet — “Alanlar ve __init__ Metodu” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Mojo Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Mojo Academy kursu toplamda 4 dersten oluşur.

“Alanlar ve __init__ Metodu” dersinde ne öğreneceğim?

Bir struct'ın durumunu başlatın. Mojo Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Mojo Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Mojo Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.

“Alanlar ve __init__ Metodu” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Mojo Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Mojo Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Bir struct Tanımlama
  2. Alanlar ve __init__ Metodu
  3. Bir struct'a Metot Ekleme
  4. Struct'lar ve Python Sınıfları
← Mojo Academy Sayfasına Dön