C#
PY

C# to Python

10 lessons

Progress0%
1Introduction2Type Systems3Functions4Collections5Object-Oriented Programming6LINQ to Comprehensions7Async Programming8Ecosystem9Decorators and Metaprogramming10Error Handling
All Mirror Courses
C#
PY
Object-Oriented Programming
MirrorLesson 5 of 10
Lesson 5

Object-Oriented Programming

Object-Oriented Programming

Introduction

In this lesson, you'll learn about object-oriented programming 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 object-oriented programming.

PY
In Python:

Python has its own approach to object-oriented programming, 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
class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self._age = age  # convention: _ = "private"

    @property
    def age(self) -> int:
        return self._age

    @age.setter
    def age(self, value: int):
        self._age = value if value > 0 else 0

Comparing to C#

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

C#
C# (What you know)
class Person {
    public string Name { get; set; }
    private int _age;

    public Person(string name, int age) {
        Name = name;
        _age = age;
    }

    public int Age {
        get => _age;
        set => _age = value > 0 ? value : 0;
    }
}
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python __init__ replaces the C# constructor

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

self is the explicit first parameter of every instance method (like 'this' but required)

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

@property decorator replaces C# property getter

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

@propname.setter decorator replaces C# property setter

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

No access modifiers — use _ prefix convention for 'private' fields

Step-by-Step Breakdown

1. Class and Constructor

Python uses __init__ as the constructor. The first parameter 'self' is like C#'s implicit 'this', but must be written explicitly in every method.

C#
C#
class Car {
    string Model;
    int Year;
    public Car(string model, int year) {
        Model = model;
        Year = year;
    }
}
PY
Python
class Car:
    def __init__(self, model: str, year: int):
        self.model = model
        self.year = year

car = Car("Tesla", 2024)  # no 'new' keyword!
Common Pitfall
Forgetting 'self' as the first parameter causes a TypeError when calling the method. The error message is cryptic — it looks like an argument count mismatch.

2. Properties with @property

Python @property turns a method into a read-only property. Add a @name.setter to make it writable. This maps directly to C# get/set.

C#
C#
public int Age {
    get => _age;
    set => _age = value >= 0 ? value : 0;
}
PY
Python
@property
def age(self) -> int:
    return self._age

@age.setter
def age(self, value: int):
    self._age = value if value >= 0 else 0

# Usage is same as accessing a field:
person.age = 30  # calls setter
print(person.age)  # calls getter

3. Inheritance

Python inheritance looks similar to C#. The super() call replaces base.MethodName() or base().

C#
C#
class Animal {
    public virtual void Speak() =>
        Console.WriteLine("...");
}
class Dog : Animal {
    public override void Speak() =>
        Console.WriteLine("Woof!");
}
PY
Python
class Animal:
    def speak(self):
        print("...")

class Dog(Animal):
    def speak(self):
        print("Woof!")

    def __init__(self, name):
        super().__init__()  # call parent __init__
        self.name = name

4. Dataclasses — Replacing Simple POCOs

Python @dataclass automatically generates __init__, __repr__, and __eq__ — similar to C# record or a simple POCO with auto-properties.

C#
C#
record Point(int X, int Y);
// auto: constructor, ToString, Equals, GetHashCode
PY
Python
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p = Point(1, 2)
print(p)          # Point(x=1, y=2)
p1 = Point(1, 2)
p2 = Point(1, 2)
print(p1 == p2)   # True (value equality)
Rule of Thumb
Use @dataclass for data containers. It eliminates boilerplate just like C# records, and frozen=True makes it immutable.

Common Mistakes

When coming from C#, developers often make these mistakes:

  • Python __init__ replaces the C# constructor
  • self is the explicit first parameter of every instance method (like 'this' but required)
  • @property decorator replaces C# property getter
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

  • __init__ is the constructor; self is the explicit 'this'
  • @property and @name.setter replace C# property syntax
  • No access modifiers — _ prefix is the 'private' convention
  • @dataclass generates boilerplate, similar to C# records
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