JS
PY

JavaScript to Python

10 lessons

Progress0%
1Variables and Constants2Functions3Arrays vs Lists4Objects vs Dictionaries5Classes and OOP6Modules and Imports7Array Methods vs Comprehensions8Error Handling9Async Programming10File I/O
All Mirror Courses
JS
PY
Classes and OOP
MirrorLesson 5 of 10
Lesson 5

Classes and OOP

Object-oriented programming patterns

Introduction

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

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with object-oriented programming patterns.

PY
In Python:

Python has its own approach to object-oriented programming patterns, 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 Animal:
    def __init__(self, name, sound):
        self.__name = name   # private (name mangling)
        self.sound = sound

    def speak(self):
        return f"{self.__name} says {self.sound}"

    @property
    def name(self):
        return self.__name


class Dog(Animal):
    def __init__(self, name):
        super().__init__(name, "Woof")

    def fetch(self, item):
        return f"{self.name} fetched {item}!"


dog = Dog("Rex")
print(dog.speak())

Comparing to JavaScript

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

JS
JavaScript (What you know)
class Animal {
  #name; // private field

  constructor(name, sound) {
    this.#name = name;
    this.sound = sound;
  }

  speak() {
    return `${this.#name} says ${this.sound}`;
  }

  get name() { return this.#name; }
}

class Dog extends Animal {
  constructor(name) {
    super(name, "Woof");
  }

  fetch(item) {
    return `${this.name} fetched ${item}!`;
  }
}

const dog = new Dog("Rex");
console.log(dog.speak());
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

__init__ is Python's constructor

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

self is explicit in every method (no 'this' magic)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Double underscore __ for private (name mangling)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

@property decorator for getters

Step-by-Step Breakdown

1. The constructor

Python uses __init__ as the constructor. The first parameter is always self (the instance).

JS
JavaScript
constructor(name) {
  this.name = name;
}
PY
Python
def __init__(self, name):
    self.name = name
Common Pitfall
Forgetting self in __init__ or any instance method causes a TypeError.

2. Inheritance

Python inherits by putting the parent class in parentheses. super().__init__() calls the parent constructor.

JS
JavaScript
class Dog extends Animal {
  constructor(n) { super(n,"Woof"); }
}
PY
Python
class Dog(Animal):
    def __init__(self,n):
        super().__init__(n,"Woof")

3. Properties

Python's @property decorator creates a getter that can be accessed like an attribute.

JS
JavaScript
get name() { return this.#name; }
PY
Python
@property
def name(self):
    return self.__name
Rule of Thumb
Use @property for read-only computed attributes; use plain attributes for simple data.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • __init__ is Python's constructor
  • self is explicit in every method (no 'this' magic)
  • Double underscore __ for private (name mangling)
Common Pitfall
Don't assume Python works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • __init__ = constructor, self = this
  • class Dog(Animal) for inheritance
  • @property for getters
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Python to practice these concepts.
PreviousNext