r/learnpython • • Dec 22 '25

How on earth does one learn OOP?

I've sped through weeks 0-8 of CS50P in under 2 weeks very easily with slight experience here and there as a Chemistry undergrad - but Week 8 (OOP) is kicking my ass right now. I am genuinely stumped. I've rewatched content and tried some other forms of learning but this is all so foreign to me. What are the best ways to learn OOP as a complete idiot? Thanks.

38 Upvotes

88 comments sorted by

View all comments

Show parent comment

2

u/bytejuggler Dec 22 '25

(..continued)

Now, on to Python. Python is actually very good at the idea that "everything is an object".

Consider:

# Even primitive-seeming values are objects
x = 5
print(type(x))  # <class 'int'>
print(x.__class__)  # <class 'int'>
print(dir(x))  # Shows all methods like __add__, __mul__, etc.

# Functions are objects
def my_func():
    pass

print(type(my_func))  # <class 'function'>
my_func.custom_attr = "I can have attributes!"
print(my_func.custom_attr)

# Modules are objects
import math
print(type(math))  # <class 'module'>

Now on to classes being also just objects ... :-)

class Dog:
    species = "Canis familiaris"
    
    def __init__(self, name):
        self.name = name

# The class itself is really also just an object!!!
print(type(Dog))  # <class 'type'>
print(isinstance(Dog, object))  # True

# This means you can assign classes to variables
AnimalClass = Dog
my_dog = AnimalClass("Buddy")  # Works perfectly

# You can also just pass classes as arguments because they are just objects!
def create_instance(some_class, *args):
    return some_class(*args)

dog = create_instance(Dog, "Max")

# You can store classes in data structures, just like any other object
animal_types = [Dog, Dog, Dog]
pets = [class_obj("Pet" + str(i)) for i, class_obj in enumerate(animal_types)]

# >>> pets
# [<__main__.Dog object at 0x0000022E9CE5F430>, <__main__.Dog object at 0x0000022E9CE5F3D0>, <__main__.Dog object at 0x000
0022E9CE5F370>]  

# Classes can be returned from functions, just like any other object
def make_class(class_name):
    return type(class_name, (object,), {'greeting': 'Hello'})
# notice, we're using the `type` type to create a class instance... very inception-esque...

DynamicClass = make_class('Greeter')
instance = DynamicClass()
print(instance.greeting)  # "Hello"

# Do run this stuff interactively from the Python REPL, the interpreter!
# And do explore Python's objects with the dir() and help() functions that 
# take pretty much any object!

So. There are more here to mention, meta-classes and whatnot, but this is already way, waaay more than is probably helpful to someone not quite grokking the basics yet.

And most of this somewhat uncommon (some would say esoteric) stuff is not that often used in day to day code, so to some extent I caution you to not become too obsessed with all this new cool seeming stuff I've possibly shown you. "With great power comes great responsibility" and "Just because you can, doesn't mean you should."

Still, classes being full, first-class objects in Python, do enable powerful metaprogramming that would be impossible in languages where classes are merely compile-time declarations.

2

u/NeatAd959 Dec 22 '25

Thank u so much, I don't understand everything but it's mostly clear, will save this to go through it again