0Pricing
Mojo Academy · Lesson

Structs vs Python Classes

Why structs are static and fast.

Structs vs Python Classes is a free Mojo Academy lesson on CoddyKit — lesson 4 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.

They Look Similar

A Mojo struct and a Python class both bundle data with methods, so they feel familiar. But under the hood they are quite different. 🔍

Structs Are Static

A struct's layout is fixed at compile time. The compiler knows every field and type ahead, so it generates tight, fast code.

Classes Are Dynamic

Python classes are dynamic: you can add attributes at runtime. That flexibility is convenient but costs lookups and speed.

No Surprise Attributes

In a struct you cannot tack on a new field after definition. Mojo rejects it, which catches typos and keeps the layout honest.

Value vs Reference

Structs are value types, so assigning copies them. Python objects are references, so two names can point to the same object.

Predictable Copies

Because structs copy on assignment, changing one instance never silently mutates another. That removes a whole class of aliasing bugs.

Speed Is the Payoff

Static layout and value semantics let Mojo skip runtime overhead. The result is performance close to C for your custom types.

No Inheritance, Yet

Structs do not use class-style inheritance. Instead Mojo shares behavior through traits, which you will explore later in the path.

Typed Fields Required

Every struct field needs a declared type, while Python attributes are untyped by default. Types are what let Mojo optimize so aggressively.

var x: Int

When to Use Each

Reach for a struct when you want speed and a fixed shape. Keep Python classes when you need runtime flexibility from existing libraries.

Same Syntax, Different Engine

The takeaway: structs borrow Python's friendly syntax but run on a static, compiled engine. You write familiar code that goes fast.

struct Point:
    var x: Int
    var y: Int

Quick Check

Let us compare structs and Python classes.

Recap

Structs trade Python's runtime flexibility for a static, value-based design that runs fast. You now know when each one shines. 🎯

Frequently asked questions

Is the “Structs vs Python Classes” lesson free?

Yes — the full text of “Structs vs Python Classes” 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 “Structs vs Python Classes”?

Why structs are static and fast. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Structs vs Python Classes” 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