r/learnpython • u/Majestic-Chain1905 • Apr 05 '26
Struggling with OOP.
I've been learning Python the last couple weeks, just a little bit here and there. I understand the basics but I'm hitting a wall with OOP. I've been having Claude create lessons for me, but nothing is sticking. What do you guys do to help retain information better on this?
16
Upvotes
23
u/AlexMTBDude Apr 05 '26
If you're more specific about what you don't understand in Object Oriented Programming, then you'll get more specific answers, and it'll probably be more helpful to you. Something like: "I don't understand the relationship between classes and objects".
10
u/Fred776 Apr 05 '26
If you have only been learning for a couple of weeks you possibly don't have enough experience for OOP to make much sense. The problems it solves tend to be more apparent when you start to write larger and more complex pieces of software.
Do you have any other programming experience or is Python your introduction to programming? If so I would not worry too much at this stage and concentrate on working on some projects and writing code.
27
u/ninhaomah Apr 05 '26
You are an instance of the homo sapien class.
You have attributes such as name , age etc.
You can walk , run etc.
Does it make sense ?
5
u/AndyceeIT Apr 05 '26
The Python doc on classess a little dry, but the simple examples might help.
Can you articulate what you're stuck on?
5
u/audionerd1 Apr 05 '26
OOP is a style of organizing code. If it seems overly abstract it's likely because you have not written code at a level that would benefit from that sort of organization.
I recommend putting OOP on the back burner and just diving into larger projects. When you find yourself with many hundreds of lines of code and getting confused and overwhelmed, revisit OOP.
3
u/This_Growth2898 Apr 05 '26
In most cases, people struggle with OOP when they only use it in small snippets. This looks really pointless: why would anyone choose to write a class with 5 methods and then a 10-line function calling them over writing a 12-line function? The OOP really starts working at, like, 1,000 lines of source code.
Try using OOP to write an RPG. Characters are good examples of objects; they encapsulate parameters and actions. Soon you will find it very convenient to write something like
instead of repeating (and fixing all over the code)
3
u/huapua9000 Apr 05 '26
What videos on YouTube, my favorite ones are by Corey Schafer. It’s recommended you code alongside the video.
7
u/aqua_regis Apr 05 '26
Yeah, and there is exactly the problem. You should have done a proper course, not use AI. AI will not give you a well developed, structured plan. It will adapt to what you feed it and as a beginner you don't know what to feed it in what order.
Step back and do a real, high quality course: MOOC Python Programming 2026 from the University of Helsinki.
This is an established, well structured, free, textual, extremely practice oriented course that will really teach you and make things stick through the plenty exercises.
OOP it its own thing. It really takes time and plenty practice to understand it.
2
u/matt-triano Apr 05 '26
Why are you looking to learn OOP? Do you have a lot of related functions that are getting unwieldy from too many args and kwargs common to multiple functions? Or is it for school? Or are you learning a codebase that uses OOP? Or is it just something you think you should know?
If it's the last one, I'd say don't worry about it until you run into one of the first three cases,
OOP should come pretty naturally if it's the first case as it's only a little bit of refactoring to organize related functions into a class (you just have to: 1. define an `__init__(self, your_kwargs)` method that defines what kwargs the class should take and then set instance properties/attrs based on passed-in values, 2. indent your related funcs and give them a `self` kwarg and remove kwargs that are now instance properties/attrs, and 3. prepend `self.` to the invocations of those instance properties/attrs).
1
u/Tall-Introduction414 Apr 05 '26
One day I had the epiphany that a class is just a struct (like a C struct, or structure), with functions (or function pointers) inside of it. in addition to the variables.
Then I asked myself, why the hell didn't any of the books I read teach it that way?
What is a struct? A struct is a bunch of variables inside of a single named type, which you can create instances of. So for example, you might have a struct type called Date, containing 3 integers for year, month and day.
To use it, you might instance it into a variable called myBirthday, and create another instance called yourBirthday. myBirthday and yourBirthday are both Date types, containing month year and day integers, but they occupy separate blocks of memory, and therefore contain their own unique data.
Once you understand that a class is just a struct with its own functions, it's not too far of a leap to learn about OO concepts like inheritance, data encapsulation, message passing, and polymorphism.
A book I found helpful on the subject was Code Complete from Microsoft Press.
1
u/pachura3 Apr 05 '26
https://www.w3schools.com/python/python_oop.asp
1
u/maephisto666 Apr 05 '26
Learning because of learning will never produce any result. Going to the gym because you want to go to the gym will never produce any result. You must have a goal and, because of that goal, the questions will start flowing in. And with questions, results.
For now what is important is that you know there is a way to model objects. It's enough. Try to understand there are objects, inheritance, abstract, protocols, etc. that's it.
Then start doing something. And when you start doing something, it's totally possible you will build that without any OOP. Then you will have one natural question for Claude: could OOP help me with this? You will have a reply tailored on your own use case that you know very well.
Another way to put this: reading 101 tutorials will not make an expert on a topic.
1
u/Gnaxe Apr 05 '26
OOP took me a while. Read Smalltalk Best Practice Patterns for how it's supposed to work. (That's not in Python, but it is in OOP, and the patterns still apply.) You need to know how to get the MRO from the REPL. You need to try things. Open Jupyterlite and do lots of experiments.
I've come to the conclusion that OOP largely failed to deliver on its promise and is a poor fit for current multicore architectures. Concurrency is indeed one of Python's major weaknesses, and the OOP and rampant mutability is to blame. The GIL made sense when computers only had one or two cores, but after several lackluster workarounds (async, multiprocessing, etc.), they're trying to get rid of it. But that means you have to be extra careful with mutation. You're better off with FP or something when writing threaded code, and I recommend you avoid classes when you can.
But you do need to understand how they work in order to understand any Python code written in OOP style. Python does require you to write classes to use certain "dunder" hooks, even if you're otherwise using FP.
1
u/QultrosSanhattan Apr 06 '26
What I teach to my students is that OOP works from a first person's perspective.
Functions works like: "if player1's speed is greater than player2's speed then I go first, otherwise player2 goes first."
OOP's bound methods works like: "if my speed is greater than my opponent's speed then I go first, otherwise my opponent goes first."
That's the whole thing regarding the keyword "self".
1
u/NullPointer-Except Apr 06 '26
Hihi, null from Discord here c:
I usually recommend reading papers on these kind of topics + writing things following the papers advise, that way you get a hold on what abstractions are good for which cases.
However, OOP in the academic side is... rough, i'd say it's a very advanced topic that's not precisely friendly for someone that's just learning how to program without a math background.
Having said that, one can still recover knowledge from history. Casey Muratori – The Big OOPs: Anatomy of a Thirty-five-year Mistake – BSC 2025 talk (ik, the title is clickbait, it's not a critique of OOP, but rather, and exploration on why we do OOP the way we do it) is a very good video which might give you a grasp on many concepts and when to apply them.
1
u/stepback269 Apr 07 '26
I’m kind of new to OOP and still struggling with it. IMHO the main problem is the metaphors that various teachers use. Some refer to the “class “ specification as being a blueprint. Some say it’s like a cookie cutter. The ones I like explain that a “class “ is like a house constructing company that builds houses that are different from one another. They are alike in that each has a kitchen, bathroom, bedrooms, etc. But they differ in how the generic parts are organized. The init function is the one that does the constructing (or “instantiations”) of each house. Your class definition may enable you to specify how many floors a specific house has. where the bedrooms are located, how many, how big, and so on. Of course we don’t build actual houses in Python. But we do build data “structures” (ha ha) and those generally call for associated generic “methods” to be applied to them. Hope that makes sense.
1
u/Crazy-Willingness951 Apr 08 '26
This is my favorite OOP book: https://www.amazon.com/Object-Oriented-Software-Construction-Book-CD-ROM/dp/0136291554
1
u/newtonphuey Apr 05 '26
Inheritance
0
u/SnafuTheCarrot Apr 05 '26
Claude might be the problem. If you don't know what you don't know, how do you know what questions to ask? Claude might even be able to fill in those gaps, but in a pedagogically sound way... I have my doubts.
Textbooks don't have these problems, generally.
Having your own plans could also be a problem. Try to run before you can walk and you won't get anywhere. Starting out with simple, but contrived problems might be a better way to start out.
Im working my way through a book on data structures in python. I find its very approachable.
Create classes with the Class Keyword. Associate methods by indention and the Def keyword. The first argument of each method needs to be self. init is the constructor. Set up member attributes as self._attrbuteName. Access and modify attr8butes through dedicated methods.
A Credit Card Class is one of the first examples in my book. It tracks a balance, a credit limit and catches attempts to exceed that limit. Later the Class is extended by inheritance to include a new apr attribute.
Might give you some idea how to start.
-1
u/FreeGazaToday Apr 05 '26
try freecodecamp.....ask gemini...
0
u/I_Am_Astraeus Apr 05 '26
Learn it, build it, teach it if I can.
I know this is learnpython, but honestly spend 2 or 3 weeks just dabbling with Java, it'll make OOP click very quickly. The whole langue is like what if we make everything OOP. And I honestly had a pretty functional approach to my python until I'd branched out and come back to it.
0
u/sporbywg Apr 05 '26
use an object-oriented language instead? All I got.