Python for AIPython for AI
Python Basics

Your first class

Create a simple class

Let's explore classes in Python. Classes are a fundamental way to organize your code and data.

How classes work

Imagine a blueprint for building something. A class is just like that blueprint. It describes what attributes (data) an object will have and what actions (methods) it can perform.

Classes help you create many similar objects without rewriting the same code over and over.

Basic class structure

To define a class, you use the class keyword followed by the class name. Class names usually start with an uppercase letter.

python
class Dog:
    # Class body goes here
    pass

The pass keyword is a placeholder. It means "do nothing" and is useful when you want to define a class but haven't added any content yet.

Adding an initializer

When you create an object from a class, you often want to set it up with some initial values. This is where the initializer method comes in.

The initializer method is special; it's called __init__. It acts like a constructor, setting up your object when it's first made.

python
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.name)

Understanding self

You might have noticed self in the __init__ method. self refers to the specific instance of the class that the method is being called on.

  • It's always the first parameter in instance methods.
  • It allows methods to access the object's attributes and other methods.
  • You don't pass it explicitly when calling the method; Python does it for you.
python
class Car:
    def __init__(self, color):
        self.color = color

    def describe(self):
        return f"This car is {self.color}."

my_car = Car("blue")
print(my_car.describe())

Real-world example: configuration

Classes are super useful for bundling related settings. Imagine storing application configuration like database details or API keys.

python
class AppConfig:
    def __init__(self, db_host, api_key):
        self.database_host = db_host
        self.api_secret = api_key

# Later in your app:
settings = AppConfig("localhost", "supersecret123")
print(f"Connecting to DB at: {settings.database_host}")

Class vs instance

This can be a bit tricky at first. A class is the blueprint. An instance is a specific object created from that blueprint.

  • Class: Dog (the idea of what a dog is).
  • Instance: my_dog (your specific dog named Buddy).
python
class Person:
    def __init__(self, name):
        self.name = name

person1 = Person("Alice") # person1 is an instance
person2 = Person("Bob")   # person2 is another instance

print(person1.name)
print(person2.name)

Common mistakes

  • Forgetting self as the first parameter in instance methods.
  • Trying to access instance attributes without self. (e.g., name instead of self.name).
  • Not calling the __init__ method (though Python usually guides you here).

Practice

Try these exercises to lock in what you just learned.

Quiz
Not started
"Functions are first-class" in Python means…

What's next?

You've successfully created your first class! This is a huge step in learning object-oriented programming.

Next, you can explore adding more methods to your classes, understanding class variables, and diving into inheritance.