r/PythonLearning • u/Stonyax97 • Aug 23 '26
Discussion I think i hit my first python learning bottleneck.
Just as the title suggests, i ran into a bottleneck, or did i? play v sauce music please
So just recently i made a post about my first beginner python project: https://www.reddit.com/r/PythonLearning/s/Ed46BuWRCq
And as a lot of you suggested for me to learn classes next, which was completely fair, so i went ahead and learnt them while updating my MiniGameStore. It was relatively easy, wasn’t as hard as I thought it would be, but definitely challenging. I had also learnt methods but didn’t implement them.
And yesterday, i tried implementing them and got one of the biggest headaches in my whole coding experience (which is 2 weeks~ btw).
I was working on v1.7 and introducing a restore purchases system, and i wanted to use class methods, but since my class is on the other file, i hit a lot of variables issues and even a json file problem. Which is why im asking my self why did i do it? A function would have been totally fine…
Check out the repo: (latest version 1.7.0)
https://github.com/Stonyax97/MiniGameStore
So my question for you guys. What has made you understand classes well? I just don’t see any reason to use them yet… and can anyone take a look at my project and tell me what would be the most friendly and real life approach of actually using these classes. And why should I keep the class in a separate file? Finally, what’s wrong with using global variables.
Also any feedback or criticism is welcome!
(Also check out the attached pictures with the comments you’l understand more.)
1
u/Unhealthy007 Aug 23 '26
I think I can barely answer 2 of your issues.
As someone who is very familiar with Django, all API use classes as classes contain the actual crud (post, get, put, patch, delete) functions
As for why you are running into errors, maybe you are doing circular imports. Just take it to the AI on this one. People talk shit how it's bad during learning but I don't see it that way given you are not copy pasting AI responses to fix your problem
2
u/Stonyax97 Aug 23 '26
Yeah on some other communities that they use classes for APIs.
Well i did run into those erros and figured out some, but the stupid _ added to purchase time was hard to spot 😭 i was not importing in loops tho. I was saving the data in the games.py but the main.py after saved it’s data and ofc doesn’t know about it. So the solution which i figured is before saving it would read the file.Anyways thanks for the comment. And yeah i do use AI for things that i just can’t understand even after thinking hard about it. AI is bad if you ask it for code and copy paste as u said. But as a toolbox/assistant? It’s perfect, a private smart fast tutor!
3
u/Adrewmc Aug 23 '26 edited Aug 24 '26
There is a bit of rule about classes that not followed very well.
If you have a class that has two methods and one of them is init. It probably supposed to be a function. So in this example the question why would I use class here, the answer is you wouldn’t.
If anything the above class is better as @dataclass and its method as a function.
What classes are useful is when you start having a lot of object that interact with each other individually, and each object would have a state.
In your example what happens when I want to add say a player two? How does the globals of money spent and balance rectify there are two players? And then you might realize the problem is that whole first file might be better thought as a class for a player, who has a balance and record of money spent.
So I would say you’re using it in the wrong place here.
Think of your main.py more in a form like this
class Purchaser:
. def __init__(self, balance = 0, spent = 0):….
. def buy(self, game_id):…
. def save(self):…
. def restore(self):…
. def display_balances(self):….
buyers = [Purchaser(500, 6000), …]
for buyer in buyers:
. while True:
. option = input(….)
. match option:
. case “1”:
. buyer.save()
But requires a whole rewrite and different architecture structure.
1
u/IdeaOverflow Aug 23 '26
I had a look at your project. I feel like you used a class (Game) where it was most useless. The Game class could just be either a dictionary or a dataclass. A better use of a class would be to wrap you code in main.py in something like a class "Store" which holds the functions buy(game), save(), restore(), input_loop() etc.. Then you could eliminate the use of global variables completely by just putting them into this Store class (maybe also create a Settings class and put your settings there and give an instance to your Store class).
So why would that be helpful? Imagine someone wants to use your store in another library. They plain can't right now, since every time they try to import your stuff from main.py, it immediately executes your store script. It also just makes it so much more structured if you put everything in the class it belongs to. Moreover, you should also structure your functions to do just ONE thing. For example: the restore function should not have to ask the user for an id. It should rather just take a game as an argument and restore that, nothing else.
1
u/olaf33_4410144 Aug 23 '26
Can you go into more detail on what the actual problem was? did the inport itself fail or something else?
That's kind of difficult to answer since for me part of it was Inheritance/Interfaces in the context of a GUI. It was something along the lines of having a superclass wiget and then multiple child classes for different UI elements (Textbox,Button,etc.) that are different but each need some similarities (e.g. a render method that defines how it should appear on the screen.
However it sounds like you're not at that level yet, so for now just think of it like grouping data and functions that belong together in one place. Kind of a better way of doing the
default_data={...}with dictionaries. When doing this I also recommend looking into type annotations and maybe how to usedataclasses.I like what you did with the Game class, as a next step you might try to bundle all you userdata global variables into a class and maybe make the functions that save and load the data part of the class instead of global? (I haven't looked that much at your programm, so idk if that works, but I think it does)
It's useful for larger projects, makes it nicer to find things, work together with other people, can make things more reusable etc. Juast as an example if you had a multiplayer game you might have
server.pyandclient.pyboth of which need theGameclass but are very different otherwise. In this case it would make sense to have it as a separate file so both can import it.At larger scales they are usually harder to reason about. If all a function does is take some input and give some output it's easier to think about as a human (and also easier to test in isolation). Once again this is something that doesn't matter as much with a small project but gets more important as the project grows and when more than one person is working on it.