How would I know when to use make a new class or rather just define a new function in programing. Both applications really still confusing
it mainly depends on what stile of programming are you using, functional programming or object oriented programming
OOP ofc. Reason why i made the post under Python section
Python supports both OOP and Functional programming. If you’re doing more of a OOP focused design, then you will end up using more objects and object methods.
Y’all are not getting the point of this question.
Can someone really into Python programming explain how he/she knows when to create a class or define a function.?
I’ve literally asked ChatGPT about the differences and application, but I’m not getting the practical answer I need.
if you do OOP, define classes
I think you’ll need to explain the point of your question in a lot more detail.
I would say generally: Don’t use classes unless you need to. Then, use them.
But there’s a lot of nuance and details and “it depends” in there and differing opinions. Just use one of them until it doesn’t work and you need the other one. Eventually you will deeply understand both and know when to apply them.
There’s probably a good book about this.
You could read these for more information:
https://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming
https://www.educative.io/blog/functional-programming-vs-oop
https://careerfoundry.com/en/blog/web-development/functional-programming-vs-oop/
Specific to data analysis:
https://www.datacamp.com/tutorial/functional-programming-vs-object-oriented-programming
Python does not necessarily mean OOP (even though everything in Python is an “object” it’s not really the same idea…)
Alright thank you, here it is, I understand that a class is used to define a new data types, while a function is a block of code that gets executed when called. But I see that both needs to get called before executed. And the pattern for creating both looks the same (so far to me, and I’m really stuck in deeply understanding the concept between the two).
So if you would explain and got a in depth material that explains when to know the one to use between the two.
I’m dealing with Python and I want to learn how to use it in computational programming and ML.
I appreciate!
In the vast majority of cases, either will work, so which to use depends on the style you are using. As you are using OOP, define classes with its methods
This is a really vague question. That’s why we can only give general answers. I’d do some Googling and researching and ask some more specific questions.
I provided a few links for you. Did you already read those, and do you have more specific questions after that?
A class is like a blueprint that is used to create objects in memory.
You might have a blueprint of a car (class) and use it to build 5 cars (objects). Each car will maintain it’s own mileage or different color independently but will work the same since they have shared blueprints.
A class can have functions within it (called methods) as well as internal data that will be stored in each instantiation of that class. The car class might have a changeTires() method.
I think a function is more straightforward as you described, a block of code that gets executed when you call it. You don’t really call a class but you create an object (instantiate) from it.
So, from these examples I hope it gives you a better understanding of when you want to use each? I think this is something that will become clear to you after time spent using both classes and functions.
You can find similar explanations here:
https://forum.freecodecamp.org/t/transition-from-functional-programming-to-oop/689060/1
Generally, you’d create a new class when you need to model something with data and behavior—think of a blueprint for creating objects. Functions, however, are best for tasks or actions that don’t need to keep track of state. If you need multiple functions operating on the same data, it might be time to create a class. Otherwise, a function will do the trick.