Theory of Intelligence
PYTHON PROGRAMMING•Object Oriented Python•Classes And Objects•1.1 Classes, Objects & Attributes

1.1.1 What Is OOP

Subtopics: What Is OOP, Classes & Objects and Instance vs Class Attributes.

1.1.1 What Is OOP

OOP is a way of writing programs by modeling real things (like a student, a bank account, a shopping cart, or a game character) as objects.

An object bundles two things together:

  • Data (attributes) like name, balance, position.
  • Behavior (methods) like deposit(), move(), print_receipt().

This style becomes more helpful as your project grows, because you can keep related code in one place.


Why OOP is Useful (Beginner View)

  • Organization: instead of many loose variables and functions, you group them into a class.
  • Reusability: you define a class once, then create many objects from it.
  • Maintainability: when something changes, you usually update a single class rather than many scattered places.
  • Safety: you can add checks inside methods so your data doesn’t become invalid.

The Basic Vocabulary

  • Class: a blueprint (definition).
  • Object (instance): a real thing built from the blueprint.
  • __init__ (constructor): runs when an object is created; used to set starting values.
  • Instance attributes: values that belong to one specific object.
  • Methods: functions that belong to a class and operate on an object’s data.

A Real, Doable Example: A Particle You Can Simulate

This is a small example you can actually run and extend later (physics, games, animations).

PYTHON
import math


class Particle:
    def __init__(self, x, y, vx, vy, mass=1.0):
        self.x = float(x)
        self.y = float(y)
        self.vx = float(vx)
        self.vy = float(vy)
        self.mass = float(mass)

    def speed(self):
        return math.sqrt(self.vx ** 2 + self.vy ** 2)

    def move(self, dt=1.0):
        self.x += self.vx * dt
        self.y += self.vy * dt

    def apply_force(self, fx, fy, dt=1.0):
        # F = m*a  =>  a = F/m
        ax = fx / self.mass
        ay = fy / self.mass
        self.vx += ax * dt
        self.vy += ay * dt


p = Particle(x=0, y=0, vx=3, vy=4, mass=2)
print(p.speed())  # 5.0

p.apply_force(fx=2, fy=0, dt=1)
p.move(dt=2)
print(p.x, p.y, p.vx, p.vy)

What to notice:

  • p is an object.
  • x, y, vx, vy are attributes stored inside the object.
  • move() and apply_force() are methods.
  • All particle-related logic stays inside Particle.

What You’ll Learn Next (Roadmap)

  • Classes & objects (designing and creating them)
  • Encapsulation (protecting data; exposing safe methods)
  • Inheritance (reusing and extending existing classes)
  • Polymorphism (same method name, different behavior)
  • Abstraction (hiding details and exposing a clean interface)

Object-Oriented Programming (OOP) is a way of designing and organizing code to make it easier to manage and understand.

Class and Objects

A class is like a template for creating things, and an object is an actual thing created from that template. Imagine you want to make a toy car. You first need a blueprint or a template that describes how the toy car should look and function. This template includes details like:

  • Color
  • Size
  • Number of wheels
  • Type of material

The template is not an actual toy car; it's just a plan and it represents a class. Using the template, you can create multiple toy cars. Each object is an instance of the class, meaning it follows the plan to have its own specific characteristics.

Defining Classes and Creating Objects

In programming, we use classes as concepts to define what an object should be like.

PYTHON
# Define a class called ToyCar
class ToyCar:
    # The __init__ method initializes the object with specific attributes
    def __init__(self, color, size, wheels):
        self.color = color   # Color of the toy car
        self.size = size     # Size of the toy car
        self.wheels = wheels # Number of wheels in the toy car

    # Method to describe the toy car
    def describe(self):
        return f"This toy car is {self.color}, size {self.size}, and has {self.wheels} wheels."

# Create objects of the ToyCar class
car1 = ToyCar("red", "small", 4)
car2 = ToyCar("blue", "large", 6)

# Print descriptions of the toy cars
print(car1.describe())
print(car2.describe())

Explanation:

  • Class Definition: The "ToyCar" class is like the template for making toy cars. It describes what attributes a toy car should have: color, size, and wheels.
  • Creating Objects: "car1" and "car2" are specific toy cars object created using the ToyCar template. Each has its own unique attributes.
  • Using Methods: The describe() method allows us to get a description of the toy car.
  • Self: self is a convention used in Object-Oriented Programming (OOP) to represent the instance of a class within its methods.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!