> ## Documentation Index
> Fetch the complete documentation index at: https://python4ai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Classes

> Create your own data types

## What is object-oriented programming?

Object-oriented programming (OOP) is a way to organize code by grouping related data and functions together. Instead of having separate variables and functions scattered around, you bundle them into objects.

Think of it like organizing a toolbox:

* **Without OOP**: Tools scattered everywhere
* **With OOP**: Tools organized in labeled compartments

<Note>
  Classes are a more advanced Python concept. Don't worry if they feel confusing at first - they'll click with practice. Many Python programs work perfectly fine without classes, but they become valuable as your projects grow.
</Note>

## What is a class?

A class is a blueprint for creating objects. It defines:

* **Attributes**: What data the object stores
* **Methods**: What the object can do

```python theme={null}
# Without classes - data and functions separate
name = "OpenAI"
model = "gpt-4o-mini"

def generate_response(prompt):
    # Process prompt...
    return response

# With classes - everything bundled together
class OpenAIClient:
    def __init__(self, name, model):
        self.name = name
        self.model = model
    
    def generate_response(self, prompt):
        # Process prompt...
        return response
```

## Why use classes?

Classes help you write more understandable programs as they grow. Here's the typical progression of a Python developer:

**1. Single file scripts** (where you started):

```python theme={null}
# everything.py - All code in one file
api_key = "sk-..."
prompt = "Explain Python"
response = make_api_call(api_key, prompt)
print(response)
```

**2. Functions** (where you are now):

```python theme={null}
# main.py - Organized with functions
def setup_api(key):
    return {"key": key, "base_url": "https://api.openai.com"}

def generate_response(api_config, prompt):
    # Make API call
    return response

api = setup_api("sk-...")
result = generate_response(api, "Explain Python")
```

**3. Multiple files** (getting organized):

```python theme={null}
# api_utils.py
def setup_api(key):
    return {"key": key, "base_url": "https://api.openai.com"}

# main.py
from api_utils import setup_api
api = setup_api("sk-...")
```

**4. Classes** (where we are now):

```python theme={null}
# client.py
class OpenAIClient:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = "https://api.openai.com"
    
    def generate(self, prompt):
        # All logic encapsulated here
        return response

# main.py
from client import OpenAIClient
client = OpenAIClient("sk-...")
response = client.generate("Explain Python")
```

**In the context of AI**, classes specifically help you:

* Build clean interfaces to APIs
* Manage complex data pipelines
* Create reusable components
* Organize stateful operations

<CardGroup cols={2}>
  <Card title="Your first class" icon="hammer" href="/advanced/classes/first-class">
    Create a simple class
  </Card>

  <Card title="Methods and attributes" icon="wrench" href="/advanced/classes/methods-attributes">
    Add functionality to classes
  </Card>

  <Card title="Inheritance" icon="sitemap" href="/advanced/classes/inheritance">
    Build on existing classes
  </Card>

  <Card title="When to use classes" icon="lightbulb" href="/advanced/classes/when-to-use">
    Best practices and guidelines
  </Card>
</CardGroup>
