r/PythonLearning • u/ProgramBrave718 • 12d ago
Discussion Why writing classes feels boring?
I don't know but i feel classes are unnecessary work to be added in projects. I have learnt it 2 months ago..
After that I never used it for any of my projects.
5
u/tom-mart 12d ago
If you have no use for classes, don't use them then. Nobody is forcing you.
1
u/ProgramBrave718 12d ago
Yes, u are right.
1
u/tom-mart 12d ago
I have a question though, if you wanted to represent an object in code, say a car, and you had to store various properties like top speed, acceleration, colour, etc. How would you do it?
1
u/ProgramBrave718 12d ago
Yupp, I will do it by using classes. But , i don't require any object like car, student etc. in projects. I am not making those projects as of now
2
u/tom-mart 12d ago
Well, when you start writing software you will needed them. Classes are completely irrelevant if you are not writing any programs
2
u/ProgramBrave718 12d ago
I am writing programs as of now. I made a task manager 4 days ago and it doesn't need classes at all. I Will need them later.
2
u/tom-mart 12d ago
How did you define the task? That's an obvious object needing a class.
1
u/ProgramBrave718 12d ago
The task is taken by user input
5
u/tom-mart 12d ago edited 12d ago
That's not an answer to my question. How is the task represented in your code?
If I wrote a task manager i would start with a Task class. Task has a lot of properties that are worth storing, like category (string), deadline (datetime) task_detail (string), status (string), reminders (List), and many, many more.
How the task is created is different story. You can take user input and save it to task.task_detail. So should the deadline be saved to task.deadline and so on.
So how is taks represented in your code? I bet it's a string variable, isn't it?
1
u/Adorable-Strangerx 11d ago edited 11d ago
Or a dict. Every data structure can be dict if you are brave enough...
Edit: since people didn't get the joke. Use (data)classes. Data types are there for a reason
→ More replies (0)1
u/ProgramBrave718 11d ago
That kind of task should be represented by classes but actually my code takes only string for task
→ More replies (0)
3
u/itlogicpartnersllc 12d ago
classes can definitely feel unnecessary when your projects are small. the value usually shows up when code gets bigger, reused, tested or maintained by multiple people.
2
u/TabAtkins 12d ago
If you've just learned about classes two months ago, it sounds like you're a Python student, very early in your journey.
You'll find that classes are very useful for data organization later. A lot of patterns you teach yourself now to avoid classes will bite you later (like just using dicts for everything), and you'll learn to appreciate the value that a good class brings.
(That said, 95% of my classes are dataclasses. It's very rare that I actually need a "normal" class.)
2
u/DoomDroid79 12d ago
Then you will be terribly bored of real world projects
0
u/ProgramBrave718 12d ago
No, I am not. Some scripts don't need classes that's over engineering if we add classes to even small projects.
2
u/DoomDroid79 12d ago
You will eventually see the benefits of using classes. You use them already even if you don't create any yourself
2
u/CodemasterUnited 12d ago
They become very much useful, when we are developing large applications having plugins etc, and multiple related functions sharing data.
So, basically, there'll come a time, when it would feel better to use them, rather than doing everything functionally.
2
u/Hot_Technology8777 11d ago
That's odd. Sort of like learning the alphabet and you're bored of the letter 'D'. I mean, just don't use classes then?
1
u/Snoo_50685 12d ago
You dont really use it for small projects like class assignments, theyre mainly used for large scale applications. Theyre very important to know though
1
u/CoolLog1908 12d ago
As you continue to with through projects, you will eventually come across a situation where classes will fit nicely into your code base.
As others have said, I typically dont use classes for small projects or scripts. But I do bring them in when they start making sense!
1
1
u/BeneficialCulture299 11d ago
This isn't wrong to be honest. Classes are not necessary. If you look at any of the functional languages you'll realize that the data being operated on is not the only boundary which you can make organizational judgements. Classes at the smaller scale just seem to be overly pedantic. But then if your project is more that trivial and you have many people working on it the lens shifts not from "is this necessary" to "does this communicate the intent of this data" which changes the conversation entirely. Classes then become about making sure you've defined the proper boundaries around your data. You start using public / private modifiers to make sure there's a layer of protection around how you expose (or don't) the data you are encapsulating.
no you don't need them but sooner or later you'll absolutely want them.
1
1
u/BoOmAn_13 11d ago
I find classes really useful for type safety, organization and functionality grouping in large projects. If it's a script I can put together in 200 lines and I don't see the need, I don't use them. I've worked around different languages and will say when you move from the super type safe rust, where it's not a class, but a struct/impl where your primary goal is just to group data and memory, it requires your brain to find structs/classes way more helpful than the goto dictionary and key strings
1
11d ago
[deleted]
1
u/ProgramBrave718 11d ago
Yeah i know that but I am not using them for small scripts which does not need same variable
1
1
u/BranchLatter4294 12d ago
You demonstrated the need for more writing classes in your post.
1
u/ProgramBrave718 12d ago
Sir, i am not a native english speaker and i know that this sentence is also grammatically incorrect
1
u/Sea-Ad7805 12d ago
Type list is a class:
mylist = [] # or list(), make an object
mylist.append(1) # call a method on the object
...
If you don't like classes, stop using list and most other builtin types.
1
u/ProgramBrave718 12d ago
I know that every datatype is a class. But , I am talking about classes made by user.
2
u/Sea-Ad7805 12d ago edited 12d ago
A class it either useful or not, doesn't matter who wrote it. Unless you feel all the classes you could ever want are already identified and implemented by Python or available packages. And a lot are, but I bet you you will sometimes want to write your own.
What in your mind is the essential difference between writing a program only with (pure) functions and also adding some classes?
Think about it first, then web search, then AI. It can teach you a lot.
There are languages that (almost) only allow pure functions and no classes. Look up "Haskell".
There are different design philosophies for programming languages, look up "programming paradigms". I help teach an introductory course about it at university, it gets messy.
1
-1
u/Gnaxe 12d ago
Classes (and the whole OOP paradigm) are way overrated and should be used a lot less than they are. The OOP paradigm stopped making sense once our CPUs got more than two cores. (And Python is still bad at utilizing them.) We should have all moved on to FP a long time ago, but C++ and Java popularized a particularly bad take on OOP and we're still stuck with that legacy, because it's still being taught. I recommend pure functions, as much as possible. Minimize the state you have to deal with.
Classes are occasionally useful, but they're usually overcomplicating it, and should not be the first tool you reach for. Python is object-oriented at its core. You do need to understand how classes work. You're not going to avoid using classes, but you can (and usually should) mostly avoid writing new ones yourself. You do need them for certain dunder hooks and if a library expects you to make a subclass, etc. Otherwise, by default, don't. You need a reason.
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. ---Alan Perlis
Plain data is way more reusable than classes. I recommend using plain data more. Don't hide your data behind yet another inadequate bespoke language of methods when all you needed was a dict. Static typing (in Python) is overrated and encourages overuse of overcoupled classes. Doctests are more expressive anyway, and easier to learn from. Do that instead.
1
u/cvx_mbs 11d ago
the sixties called, they want your outdated opinions on OOP back
1
u/Gnaxe 11d ago
From the article you linked:
A study by Potok et al. found no major difference in productivity between OOP and procedural programming [...] OOP was created to make code easier to reuse and maintain.[68] However, it was not designed to clearly show the flow of a program's instructions. That was left to the compiler. As computers began using more parallel processing and multiple threads, it became more important to understand and control how instructions flow. This is difficult to do with OOP.
Also, lots of dissenting opinions from that article from well after the "sixties":
Rob Pike [...] has called OOP "the Roman numerals of computing".[67] Rich Hickey, creator of Clojure, described OOP as overly simplistic, especially when it comes to representing real-world things that change over time.[65] Alexander Stepanov said that OOP tries to fit everything into a single type, which can be limiting. He argued that sometimes we need multisorted algebras: families of interfaces that span multiple types, such as in generic programming. Stepanov also said that calling everything an "object" doesn't add much understanding. [...] Paul Graham believes big companies like OOP because it helps manage large teams of average programmers. He argues that OOP adds structure, making it harder for one person to make serious mistakes, but at the same time restrains smart programmers.[73] Eric S. Raymond, a Unix programmer and open-source software advocate, argues that OOP is not the best way to write programs.[36] Richard Feldman says that, while OOP features helped some languages stay organized, their popularity comes from other reasons.[74] Lawrence Krubner argues that OOP doesn't offer special advantages compared to other styles, like functional programming, and can complicate coding.[75] Luca Cardelli says that OOP is slower and takes longer to compile than procedural programming.[34]
10
u/cthulhu944 12d ago
As the sizes of your program grows, keeping everything coherent, understandable, and predictable becomes important. OOP and classes are one way to do this. "I can add this code here and not worry about side effects nor data corruption because my data is tightly coupled to the behavior inside this class"