r/learnpython • u/JelloEducational7428 • 4d ago
How to learn Python and libraries like PyTorch and does it make sense?
Hi, I'm a robotic engineer. At university, I never took any real programming courses except for one in my bachelor's degree where we learned C (if-else statements, loops, variables, etc.), with just a brief mention of object-oriented programming. During my master's degree, programming was never taught to me, except for some work in MATLAB and a bit of basic Python like NumPy and other minor things.
Over the past few months, I've been involved in a project where I had to build upon the work of other colleagues and use Object-Oriented Python, since it involved Reinforcement Learning. I ran into several problems:
- Python object-oriented programming, which I had never seen before;
- various scripts written by people before me;
- the use of PyTorch and other libraries.
On a theoretical level, I more or less knew what I was supposed to do, but practically I didn't know how to implement it—for instance, how to create a neural network using PyTorch. I figured it out by relying heavily on LLMs, asking a ton of questions, and pasting my colleagues' old code.
However, the problem was that every function or class I tried to understand was like a Matryoshka doll, containing more stuff inside it. Between all the files, declared variables, and created instances, after a while, I would get confused. One of the most frequent issues I encountered was lines of code like car.engine.turn_on["cylinder"].execute(). I struggled a lot to figure out whether I was dealing with folders/files or objects that had other objects as attributes inside them. Moreover, I discovered that sometimes in Python, certain functions like __call__ or __getitem__ are executed automatically based on the brackets you use, which confused me even more.
Without rambling too much, I'd like to ask you:
- In your opinion, what is the best way to properly learn object-oriented programming in Python and libraries like PyTorch?
- Does it make sense to study in detail what a function does internally, see that it calls a superclass method, and understand how the latter is implemented?
- What level of detail should one aim for, and do you think I expected too much of myself?
- And above all, with the advent of these high-performing AIs, does it make sense to struggle so much, or is it better to delegate to an AI by giving it the right instructions on what to do?
In these situations, I feel overwhelmed seeing that, for example, Claude does a myriad of things in much less time than me, and above all, things that work, whereas I don't know how long it would take me to debug.
2
u/LayotFctor 4d ago edited 4d ago
Sounds like you already have the basics. OOP is just one chapter you force yourself to get through, it takes a day to know what it is in theory, longer to understand in practice. That said, you're literally surrounded by OOP code, so it shouldn't take long for you at all. Like a week maybe?
Best way to learn is definitely to write it by hand. Most learning resources have exercises, do them without AI.
The dunder methods with double underscores are python specific internal machinery, not programming in general. But since python is used so much in your field, it's worth it. You don't have to learn all dunder methods, just what they, and a quick glance through a list of them to see what they're capable of.
Whether it's worth it is up to you. It really helps, even with AI, to at least have a deeper understanding of what's going on. It kinda sucks to have something you just permanently don't understand.
2
u/Agawell 4d ago
The best way to learn python (and any libraries) is to write it… build some simpler projects 1st
The best way to look at OO is by describing things - everything is an object, belonging to a class, everything has properties and functions that can be utilised by things external to it and it has relationships with other objects - through inheritance of properties and functions or by having other objects inherit from it
Take for example a mug… its a drinking vessel, which is a subclass of vessel (something that contains something else) because it inherits a lot of properties and functions from vessel
What specifically makes it a mug? Material, volume, dimensions, has_handle, drink_from() etc
Now look at a cup… a subclass of mug specifically one with smaller values for volume and dimensions - ie it’s smaller
These are all things we do subconsciously - OO is just a way of documenting it
Once you get that into your head and can describe anything and everything as an instance of a class and the relationships that it has with other things, then you can move on to more abstract ideas
Does it make sense to study what 3rd party classes do internally… maybe - but often only if you need to fix an issue with it or make something that surpasses it - most of the time we use objects in the same way as we’d use a mug specifically- subconsciously
Are you expecting too much of yourself - probably
Does it make sense to learn anything in the age of ai? It’s debatable, to learn something because you like it and want to learn about it is always a good thing though - otherwise your brain, which is supposed to constantly learn new stuff, will turn to mush
2
u/baubleglue 2d ago
Spend few days reading https://docs.python.org/3/tutorial/index.html and playing with it. With C background it shouldn't be hard.
IMHO try not to "learn object-oriented programming in Python and libraries", but split it to
When you learn something like PyTorch, there is always like Get Started
Making some random unrelated project, like coding a web app with Flask (welcome & login pages), may clear your head.
1
u/ohnoitssobig 4d ago
I was working with large
shitcodebases for quite some time now: in python and C. I was never an expert in C though.It helps to focus on a specific task or objective that you want to achieve. If I needed to dump some volumetric data into some file I just went there into the code, opened a new file and dumped the data without trying to make a proper function out of it. I do not actually need to properly understand the build system, Makefiles or namespaces. I just need to make sure it compiles and does the job.
Of course, if you ARE interested in learning python you should do that but not on these kind of tasks. Grinding through the existing, and often low-quality code is developing a different skillset to be honest. You just learn a bunch of tools that help get the job done like grep, breakpoints, static analysis (go to definition, find usages), etc. If you follow Claude prompts you will see that it greps and prints a lot in python.
If AI can make the job done sure for it. If I see that I can prompt "right instructions" I just do that but that implies I know ins and outs already at this point.
1
u/ee_control_z 4d ago
Here is my response that I just provided another OP:
I strongly recommend this book. Think of it like a book that has a built-in curriculum. The flow of the material is ideal as learning builds on learning - excellently structured.
Learning Python, 6 Ed, Mark Lutz
It covers just about everything you need to know about Python from beginner level to advanced and everything in between including object-oriented programming. It is very well written so it is easy to understand and follow along.
Have your editor open to work out problems as you're reading - there are tons - so a lot of practice.
1
u/kilkil 3d ago
it depends. if it is 3rd-party code (e.g. a PyTorch function), then it would be preferable to save time and reach their documentation (they probably have a documentation website online somewhere). if it is code from your own project, or from a coding project one of your coworkers made (and presumably didn't document very well, since people almost never document things properly for internal use), then yeah you might need to read though the code structure. this is something AI can help you with — ask it to help you understand/explain/navigate the code structure.
re: learning Python, since you already have a background in programming (with C) my suggestion is to just keep doing what you're doing. What you're doing right now is a great learning exercise. Alternatively I would suggest reading the Python official docs, which actually have some OOP introductory explanations and links to further resources. And in general, when you run into something you haven't seen before, google it and prefer checking the official Python docs.
re: AI, yeah you could just ask it to do stuff for you. But if you don't have the right setup (harness, skills, etc) and if you aren't using a sufficiently large (i.e. expensive) frontier model, you will be more likely to get a bunch of slop (i.e. buggy, poorly written code). so just keep in mind that, with the current state of AI, your mileage may vary wildly. And the only way you can really validate the output is by reviewing the generate code, using static type checking, and writing unit tests. For all 3 things you need to be comfortable reading and writing the language you are using.
1
u/AlsonTeteuil-84 3d ago
delegating everything to AI works fine until it hallucinates a subtle bug in your training loop and you dont know enough to spot it. you still need a solid grasp of the fundamentals just to verify the output and debug when things go sideways
1
u/TheRNGuy 3d ago
You'll remember stuff that you use a lot.
0
u/stepback269 4d ago
Wow. You're in for a heap load of s**t. That's because EVERYTHING in Python is an OOP object. Even the most fundamental elements like integers, strings, lists, etc. are objects. Modules are objects. A simple "assignment" statement like this one:
pointer_object = pointed_to_second_object
--creates two objects: the pointer and the target
You are probably used to seeing it in the below form in the beginner tutorials though:
my_name = "Jello"
print (f"Hello {my_name}")
So hopefully you have already mastered all the fundamentals and now you have to do a deep dive into the OOP world: What is a "class"? What is an "object"?
Truth be told, I'm no expert. Just another lost soul looking to learn Python. Nonetheless, you might benefit from one of my posts (here) where I started asking all those questions about OOP myself. Hey, at least it's a place to start from.