C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
Structs → Classes & Dicts
MirrorLesson 4 of 10
Lesson 4

Structs → Classes & Dicts

Grouping related data together

Introduction

In this lesson, you'll learn about structs → classes & dicts in Python. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with grouping related data together.

PY
In Python:

Python has its own approach to grouping related data together, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int
    score: float

    def greet(self) -> str:
        return f"Hi, I'm {self.name}"

alice = Person("Alice", 30, 9.5)
print(alice.greet())
print(alice)  # Person(name='Alice', age=30, score=9.5)

# Or use a plain dict for simple data
person = {"name": "Alice", "age": 30}
print(person["name"])

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
    float score;
} Person;

Person create_person(const char *name, int age, float score) {
    Person p;
    strncpy(p.name, name, sizeof(p.name));
    p.age = age;
    p.score = score;
    return p;
}

void print_person(const Person *p) {
    printf("Name: %s, Age: %d\n", p->name, p->age);
}

Person alice = create_person("Alice", 30, 9.5f);
print_person(&alice);
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C structs are pure data; Python classes add methods

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python @dataclass auto-generates __init__, __repr__, __eq__

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python dicts can replace simple C structs for dynamic data

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

No typedef needed in Python — class defines the type directly

Step-by-Step Breakdown

1. Dataclass

Python's @dataclass decorator generates the constructor and string representation automatically, replacing C's manual struct initialization.

C
C
typedef struct { char name[50]; int age; } Person;
PY
Python
@dataclass
class Person:
    name: str
    age: int

2. Methods

Python classes combine data and behavior; C requires separate functions that take a struct pointer.

C
C
void print_person(const Person *p) { ... }
PY
Python
def greet(self) -> str:
    return f"Hi, I'm {self.name}"

3. Dictionaries

For quick, dynamic groupings without a formal class, Python dicts are a lighter alternative to defining a struct.

PY
Python
config = {"host": "localhost", "port": 8080}
print(config["host"])

Common Mistakes

When coming from C, developers often make these mistakes:

  • C structs are pure data; Python classes add methods
  • Python @dataclass auto-generates __init__, __repr__, __eq__
  • Python dicts can replace simple C structs for dynamic data
Common Pitfall
Don't assume Python works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C struct → Python class or dict
  • @dataclass generates __init__ and __repr__ automatically
  • Classes can have methods; C structs cannot
  • Dicts work for dynamic, untyped key-value data
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Python to practice these concepts.
PreviousNext