Objects

🧱 Objects are instances of classes. They bundle data (attributes) and functionality (methods) into reusable blueprints. Object-oriented programming (OOP) helps organize and scale complex programs.

1 Classes and Objects

Use class to define a class. Then, create an object (an instance of that class).

class Model:
    def increment(self):
        print("There's nothing to increment!")

model = Model()
model.increment()  # There's nothing to increment!
There's nothing to increment!

2 The __init__ Method

Use __init__ to initialize an object with data when it’s created.

class Model:
    def __init__(self, number):
        self.number = number

    def increment(self):
        self.number += 1 
        print(f"The new number is {self.number}!")

model = Model(5)
model.increment()  # The new number is 6!
The new number is 6!

3 Attributes

Objects can store data in attributes accessed using dot notation.

class Model:
    def __init__(self, number):
        self.number = number

model = Model(5)  
print(model.number)  # 5

model.new_number = 3
print(model.new_number)  # 3
5
3

4 Methods

Functions defined inside a class are called methods. They usually take self as the first argument.

class Model:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1

model = Model()
model.increment()
model.increment()
print(model.count)  # 2
2

5 Object Identity

Each object is unique and has its own identity in memory.

class Model:
    def __init__(self, number):
        self.number = number

model1 = Model(5)
model2 = Model(5)

print(model1 == model2)  # False         
print(model1.number == model2.number)  # True  
False
True

6 Class vs. Instance Attributes

Instance attributes belong to one object. Class attributes are shared across all instances.

class Model:
    pi = 3.14159

    def __init__(self, radius):
        self.radius = radius

model = Model(2)
print(model.pi)  # 3.14159
print(model.radius)  # 2
3.14159
2

7 Inheritance and super()

A child class can inherit from a parent class. Use super() to call the parent’s methods.

class Calculator:
    def __init__(self, number1, number2):
        self.number1 = number1
        self.number2 = number2
        self.result = self.add(number1, number2)

    def add(self, number1, number2):
        return number1 + number2

class Model(Calculator):
    def __init__(self, number1, number2):
        super().__init__(number1, number2)

    def print_result(self):
        print(f"The result is {self.result}!")

model = Model(1, 1)
model.print_result()  # The result is 2!
print(model.result)  # 2
The result is 2!
2

Common Errors

Some frequent errors encountered when dealing with Objects in Python.

class Model:
    def increment():
        print("There's nothing to increment!")

model = Model()
model.increment()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 6
      3         print("There's nothing to increment!")
      5 model = Model()
----> 6 model.increment()

TypeError: Model.increment() takes 0 positional arguments but 1 was given
class Model:
    def __init__(self, number):
        self.number = number

model = Model()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 5
      2     def __init__(self, number):
      3         self.number = number
----> 5 model = Model()

TypeError: Model.__init__() missing 1 required positional argument: 'number'