Python Tutorial 0/52 lessons ~6 min read Lesson 25

    Classes & Objects

    Python classes use class + init.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python classes use class + __init__. Instance state lives on self. Methods are functions whose first arg is the instance.

    Understanding the topic

    Core concepts to understand:

    • __init__ is the constructor.
    • self is explicit (not implicit).
    • Class vs instance attributes.
    • @classmethod, @staticmethod.

    Syntax reference

    Visual flow / code:

    python
    class User:
    species = "human" # class attribute
    def __init__(self, name: str, age: int):
    self.name = name
    self.age = age
    def greet(self) -> str:
    return f"Hi, I'm {self.name}"
    @classmethod
    def anonymous(cls):
    return cls("Anon", 0)
    @staticmethod
    def is_adult(age: int) -> bool:
    return age >= 18
    u = User("Alice", 30)
    print(u.greet())

    Execution workflow

    1Classes & Objects Workflow
    1 / 4

    __init__ is the constructor

    __init__ is the constructor.

    Real-world use

    Most production Python uses dataclasses instead of hand-written __init__ for plain data containers — less boilerplate, more correct.

    Best practices

    • Prefer dataclasses for data containers.
    • Use type hints on attributes.
    • Keep classes small and focused.

    Common mistakes

    • Class attributes shared across instances — beware mutable defaults.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionClass vs instance attribute?+

    Answer

    A class attribute is defined on the class body and shared by all instances, while an instance attribute is set on a specific object, typically in __init__ via self. Attribute lookup checks the instance first, then the class. Assigning through an instance creates an instance attribute that shadows the class one, and a mutable class attribute like a list is shared and can surprise you.

    Follow-up

    What happens when you do instance.count += 1 on a class attribute?
    2IntermediateQuestion@classmethod vs @staticmethod?+

    Answer

    A @classmethod receives the class as its first argument (cls) and is often used for alternative constructors, and it works properly with subclasses. A @staticmethod receives no automatic first argument and is essentially a plain function placed in the class namespace for organization. Use classmethod when you need the class, and staticmethod when you need neither the class nor the instance.

    Follow-up

    Why would an alternative constructor use cls instead of the class name?
    3IntermediateQuestionWhy explicit self?+

    Answer

    Python makes the instance explicit so that method definitions mirror how they are called: obj.method(x) is sugar for Class.method(obj, x). This keeps the language simple, makes instance versus local variables unambiguous, and lets functions be bound as methods without special syntax. The name self is only a convention, though a strong one.

    Summary

    In summary: self is the first arg. dataclasses for data, classes for behavior.

    Ready to mark this lesson complete?Track your journey across the entire course.