Pythondex

Tutorial

Init Method in Python Classes

In the previous lesson, you learned how to create a simple class and an object. But every object had the same data.

What if we want each object to have different values? That’s where the __init__() method comes in.

What is __init__()?

The __init__() method runs automatically when you create a new object. It helps you give your object its own data.

Example

  • __init__() is called automatically when you create a new object.
  • self refers to the current object (we use it to store data inside the object).
  • Each object can have its own name and color.

Quick Recap

  • __init__() runs when you create an object.
  • It helps give each object its own data.
  • self is used to refer to the object itself.

In the next exercise, you’ll create a class and use the __init__() method yourself.