python-course.eu

4. Type Annotation Classes

By Bernd Klein. Last modified: 13 Jul 2023.

This chapter of our Python course is about annotating classes. So you will nt learn the thoery of Python classes and how to write proper Python classes. If you want to learn all about object oriented programming in Python, we recommend our course on OOP:

OOP

A Class

We saw in the following Python example a class with type annotations:

%%writefile example.py

class Rectangle:
    def __init__(self, length: float, width: float) -> None:
        self.length: float = length
        self.width: float = width

    def area(self) -> float:
        return self.length * self.width

    def perimeter(self) -> float:
        return 2 * (self.length + self.width)

r: Rectangle 
r