Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

classes-objects-python

Classes and objects

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~2 min
Level
beginner

This lesson

This lesson teaches Classes and objects: the syntax, patterns, and safety habits you need before advancing in Python.

Classes and magic methods underpin Django models and dataclasses—OOP is lighter than Java but still essential.

You will apply Classes and objects in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A class defines behavior and data; an instance is a runtime object created by calling the class. If you know Java or C# classes, Python OOP feels familiar—but everything is public by convention and self is explicit.

Defining and using a class

class User:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hi, {self.name}!"

ada = User("Ada")
print(ada.greet())

__init__ initializes instances. Methods take self as the first parameter—the instance reference.

Instance vs class attributes

Attributes on self are per-instance; attributes on the class are shared—mutable class attributes can surprise you if used as instance defaults.

Important interview questions and answers

  1. Q: What is self?
    A: Reference to the current instance passed explicitly—unlike Java's implicit this.
  2. Q: __init__ vs __new__?
    A: __new__ creates the object; __init__ initializes it—rarely override __new__ except for immutables or singletons.

Self-check

  1. What method initializes a new instance?
  2. Why is self required on instance methods?

Tip: Explicit self on methods—unlike implicit this in Java.

Interview prep

What is self?

Explicit instance reference as first parameter—caller does not pass it; Python binds automatically.

__init__ vs __new__?

__new__ creates instance; __init__ initializes—rarely override __new__ except immutables.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • self meaning?
  • @classmethod?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump