Online · Macau Peninsula

Online STEM and Robotics Club for Macau Peninsula Students

An online STEM and robotics club for Macau Peninsula students delivers live English labs on coding, sensors and projects—ideal for international and bilingual school households without inventing a local clubhouse.

STEM Macau Peninsula

1. Introduction: Initializing Object Data Attributes

When you define a class template, its objects need data attributes to hold their information. If you do not assign initial values to an object's data attributes when it is created, those values might either remain unset or take arbitrary system defaults.

To avoid unpredictability, it is best practice to explicitly define default or initial values for an object’s attributes right at the moment of creation. If the user wants to update those values later during execution, they can easily modify them.

In Python, this initialization process is handled automatically by a special method known as the constructor.

2. What Is a Constructor and Why Is It Called __init__?

  • The __init__ keyword: In Python, the constructor is written as __init__ with two leading underscores and two trailing underscores.
  • The first method called: The constructor is called a “constructor” because its job is to construct and initialize the object. Whenever a new object instance is created from a class, the constructor is always the very first method that executes.
  • Automatic execution: You never need to call obj.__init__() explicitly. When you type the class name followed by parentheses (e.g., Dog() or Car()), those parentheses represent the constructor call. Python automatically identifies the class name with parentheses and executes the __init__ method behind the scenes.

3. Example 1: Constructor with Built-In Default Values (The Dog Class)

In this pattern, the constructor assigns fixed default values directly inside its body without requiring external inputs at creation time:

class Dog:
    def __init__(self):
        # Setting a default attribute value during construction
        self.name = "Buddy"

    def bark(self):
        print(f"{self.name} says woof")

Execution Walkthrough

# Instantiating the dog object
my_dog = Dog()

# Calling the bark method
my_dog.bark()

Output:

Buddy says woof
  • Writing my_dog = Dog() runs the __init__ method automatically.
  • The constructor initializes self.name with the default string "Buddy".
  • When my_dog.bark() is called, it outputs "Buddy says woof" using the value initialized during construction.
  • If needed, the user can change this attribute value later.

4. Example 2: Parameterized Constructor with Input Arguments (The Car Class)

Rather than hardcoding default values for every object, you can pass input arguments directly into the __init__ constructor. This allows each object to be created with its own custom attribute values from the start:

class Car:
    # Constructor accepting input arguments for brand and color
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

Execution Walkthrough

You pass the specific arguments inside the parentheses when instantiating the object:

# 1. Creating car1 with brand "Toyota" and color "red"
car1 = Car("Toyota", "red")

# Printing car1 attributes
print("Car 1 Brand:", car1.brand)
print("Car 1 Color:", car1.color)

Output:

Car 1 Brand: Toyota
Car 1 Color: red
  • When Car("Toyota", "red") runs, Python routes "Toyota" and "red" directly into the __init__ constructor’s parameters.
  • The constructor assigns them internally (self.brand = brand and self.color = color), establishing those attributes for car1.

5. Modifying Attributes and Managing Multiple Instances

Attributes configured during construction remain fully mutable, and different instances maintain completely independent states:

# Modifying an attribute on car1
car1.color = "blue"

# 2. Creating an independent second object: car2
car2 = Car("Honda", "white")

# Inspecting both objects
print("Car 2:", car2.brand, car2.color)
print("Car 1 updated color:", car1.color)

Output:

Car 2: Honda white
Car 1 updated color: blue
  • car2 is instantiated with its own unique data ("Honda" and "white").
  • Modifying car1.color to "blue" updates only car1, leaving car2 unaffected.

Lesson Q&A

In Python, __init__ (written with double underscores before and after) is a special method known as the constructor. It is called a constructor because it is used to construct and initialize the object when an instance of a class is created. It is always the very first method that executes automatically upon object creation, ensuring that the object's data attributes receive proper default or initial values.

You do not call __init__ explicitly in your code. Instead, when you instantiate an object by writing the class name followed by parentheses—such as my_dog = Dog() or car1 = Car("Toyota", "red")—those parentheses represent the constructor call. Python automatically recognizes the class name and parentheses and invokes the __init__ method behind the scenes.

By defining parameters inside __init__ (e.g., def __init__(self, brand, color):), the constructor can accept input arguments when the object is instantiated. When you pass specific values upon creation (such as Car("Toyota", "red") and Car("Honda", "white")), the constructor binds those incoming values to the respective object’s internal attributes (self.brand and self.color). This allows multiple independent objects created from the same class template to start with completely different initial states.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

Coding for Kids on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

English-First STEM Club Practice for Macau Peninsula Families

Macau Peninsula families need English-first STEM that travels with relocating households. Enquire WhatsApp +92 322 4217059. No invented Macau campus or fixed membership fees on this page.

Frequently asked questions

Are online STEM and robotics club sessions in Macau Peninsula conducted in English?

Yes! All live instruction, course slides, and coding platforms are presented 100% in clear English, making the program ideal for students attending international and bilingual schools across Macau Peninsula.

What certifications and project materials do Macau students receive upon completing club modules?

Members earn official project completion certificates, digital badges, downloadable engineering workbooks, and free access to 5 certified Udemy courses for continuous home learning.

How can parents in Macau Peninsula register their children for a free trial class?

Click the WhatsApp contact button on this page, send a quick message with your child's age, and receive instant trial booking confirmation.

Explore related learning

TaipaScratch · Taipa CentralRobotics · Central HK TSTAI · Tsim Sha Tsui