Skip to content

Classes

Python classes feel very similar to TypeScript classes. You have class, a constructor, methods, and inheritance. The main things that trip up TS developers are:

  1. The constructor is named __init__, not constructor.
  2. Every instance method must declare self as its first parameter — Python does not inject this implicitly.
  3. Class attributes (shared across all instances) are declared at class body level, not inside the constructor.
TypeScript
// TypeScript
class Animal {
static kingdom: string = "Animalia"; // class attribute
name: string;
sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
speak(): string {
return `${this.name} says ${this.sound}`;
}
toString(): string {
return `Animal(name=${this.name})`;
}
}
const cat = new Animal("Cat", "meow");
console.log(cat.speak());
console.log(Animal.kingdom);
Python
# Python
class Animal:
kingdom: str = "Animalia" # class attribute (shared)
def __init__(self, name: str, sound: str) -> None:
self.name = name # instance attributes
self.sound = sound
def speak(self) -> str:
return f"{self.name} says {self.sound}"
def __repr__(self) -> str:
return f"Animal(name={self.name!r})"
cat = Animal("Cat", "meow") # no "new" keyword
print(cat.speak())
print(Animal.kingdom)

Python uses the same single-inheritance syntax as TypeScript. super().__init__(...) maps to super() in TS.

TypeScript
// TypeScript
class Dog extends Animal {
constructor(name: string) {
super(name, "woof");
}
fetch(item: string): string {
return `${this.name} fetches the ${item}!`;
}
}
const rex = new Dog("Rex");
console.log(rex.speak()); // Rex says woof
console.log(rex.fetch("ball")); // Rex fetches the ball!
console.log(rex instanceof Animal); // true
Python
# Python
class Dog(Animal):
def __init__(self, name: str) -> None:
super().__init__(name, "woof")
def fetch(self, item: str) -> str:
return f"{self.name} fetches the {item}!"
rex = Dog("Rex")
print(rex.speak()) # Rex says woof
print(rex.fetch("ball")) # Rex fetches the ball!
print(isinstance(rex, Animal)) # True
TypeScript
// TypeScript
class Counter {
private static count: number = 0;
static increment(): void {
Counter.count++;
}
static getCount(): number {
return Counter.count;
}
}
Counter.increment();
Counter.increment();
console.log(Counter.getCount()); // 2
Python
# Python
class Counter:
_count: int = 0 # underscore = "private" by convention
@classmethod
def increment(cls) -> None:
cls._count += 1
@classmethod
def get_count(cls) -> int:
return cls._count
Counter.increment()
Counter.increment()
print(Counter.get_count()) # 2
class Animal:
kingdom: str = "Animalia"
def __init__(self, name: str, sound: str) -> None:
self.name = name
self.sound = sound
def speak(self) -> str:
return f"{self.name} says {self.sound}"
def __repr__(self) -> str:
return f"Animal(name={self.name!r})"
class Dog(Animal):
def __init__(self, name: str) -> None:
super().__init__(name, "woof")
def fetch(self, item: str) -> str:
return f"{self.name} fetches the {item}!"
rex = Dog("Rex")
print(rex.speak())
print(rex.fetch("ball"))
print(f"Is Animal? {isinstance(rex, Animal)}")
print(f"Kingdom: {Animal.kingdom}")
print(repr(rex))
What is the name of the Python constructor method?
In Python, what does `self` refer to inside an instance method?
Where are class attributes (shared across all instances) declared in Python?